Skip to content

Commit

Permalink
feat(bool fn trait): implemented quantification methods for TruthTable
Browse files Browse the repository at this point in the history
  • Loading branch information
AurumTheEnd committed May 3, 2024
1 parent c7ba4eb commit 5da60b4
Showing 1 changed file with 123 additions and 7 deletions.
130 changes: 123 additions & 7 deletions src/table/traits/boolean_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::iterators::DomainIterator;
use crate::table::iterators::{ImageIterator, RelationIterator, SupportIterator};
use crate::table::TruthTable;
use crate::traits::{BooleanFunction, BooleanValuation, GatherLiterals};
use crate::utils::btreeset_to_valuation;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Debug;

Expand Down Expand Up @@ -93,16 +94,19 @@ impl<T: Debug + Clone + Ord + 'static> BooleanFunction<T> for TruthTable<T> {
todo!()
}

fn existential_quantification(&self, _variables: BTreeSet<T>) -> Self {
todo!()
fn existential_quantification(&self, variables: BTreeSet<T>) -> Self {
self.restrict(&btreeset_to_valuation(variables.clone(), false))
| self.restrict(&btreeset_to_valuation(variables, true))
}

fn universal_quantification(&self, _variables: BTreeSet<T>) -> Self {
todo!()
fn universal_quantification(&self, variables: BTreeSet<T>) -> Self {
self.restrict(&btreeset_to_valuation(variables.clone(), false))
& self.restrict(&btreeset_to_valuation(variables, true))
}

fn derivative(&self, _variables: BTreeSet<T>) -> Self {
todo!()
fn derivative(&self, variables: BTreeSet<T>) -> Self {
self.restrict(&btreeset_to_valuation(variables.clone(), false))
^ self.restrict(&btreeset_to_valuation(variables, true))
}

fn is_equivalent(&self, _other: &Self) -> bool {

Check warning on line 112 in src/table/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/table/traits/boolean_function.rs#L112

Added line #L112 was not covered by tests
Expand All @@ -118,7 +122,7 @@ impl<T: Debug + Clone + Ord + 'static> BooleanFunction<T> for TruthTable<T> {
mod tests {
use super::*;
use crate::expressions::var;
use crate::traits::Implication;
use crate::traits::{Evaluate, Implication};

#[test]
fn test_essential_inputs_all_inputs_ok() {
Expand Down Expand Up @@ -249,4 +253,116 @@ mod tests {

assert_eq!(actual, expected);
}

#[test]
fn test_existential_and_ok() {
let target = "a".to_string();
let target_set = BTreeSet::from([target.clone()]);
let input = TruthTable::from(var(target.clone()) & var("b"));

let actual = input.existential_quantification(target_set.clone());
assert!(!actual.inputs().contains(&target.clone()));

let evaluated_with_true = actual.evaluate(&btreeset_to_valuation(target_set.clone(), true));
let evaluated_with_false = actual.evaluate(&btreeset_to_valuation(target_set, false));

assert_eq!(evaluated_with_true, evaluated_with_false);
assert!(!evaluated_with_true)
}

#[test]
fn test_existential_or_ok() {
let target = "a".to_string();
let target_set = BTreeSet::from([target.clone()]);
let input = TruthTable::from(var(target.clone()) | var("b"));

let actual = input.existential_quantification(target_set.clone());
assert!(!actual.inputs().contains(&target.clone()));

let evaluated_with_true = actual.evaluate(&btreeset_to_valuation(target_set.clone(), true));
let evaluated_with_false = actual.evaluate(&btreeset_to_valuation(target_set, false));

assert_eq!(evaluated_with_true, evaluated_with_false);
assert!(evaluated_with_true)
}

#[test]
fn test_universal_and_ok() {
let target = "a".to_string();
let target_set = BTreeSet::from([target.clone()]);
let input = TruthTable::from(var(target.clone()) & var("b"));

let actual = input.universal_quantification(target_set.clone());
assert!(!actual.inputs().contains(&target.clone()));

let evaluated_with_true = actual.evaluate(&btreeset_to_valuation(target_set.clone(), true));
let evaluated_with_false = actual.evaluate(&btreeset_to_valuation(target_set, false));

assert_eq!(evaluated_with_true, evaluated_with_false);
assert!(!evaluated_with_true)
}

#[test]
fn test_universal_or_ok() {
let target = "a".to_string();
let target_set = BTreeSet::from([target.clone()]);
let input = TruthTable::from(var(target.clone()) | var("b"));

let actual = input.universal_quantification(target_set.clone());
assert!(!actual.inputs().contains(&target.clone()));

let evaluated_with_true = actual.evaluate(&btreeset_to_valuation(target_set.clone(), true));
let evaluated_with_false = actual.evaluate(&btreeset_to_valuation(target_set, false));

assert_eq!(evaluated_with_true, evaluated_with_false);
assert!(!evaluated_with_true)
}

#[test]
fn test_derivative_and_ok() {
let target = "a".to_string();
let target_set = BTreeSet::from([target.clone()]);
let input = TruthTable::from(var(target.clone()) & var("b"));

let actual = input.derivative(target_set.clone());
assert!(!actual.inputs().contains(&target.clone()));

let evaluated_with_true = actual.evaluate(&btreeset_to_valuation(target_set.clone(), true));
let evaluated_with_false = actual.evaluate(&btreeset_to_valuation(target_set, false));

assert_eq!(evaluated_with_true, evaluated_with_false);
assert!(!evaluated_with_true)
}

#[test]
fn test_derivative_or_ok() {
let target = "a".to_string();
let target_set = BTreeSet::from([target.clone()]);
let input = TruthTable::from(var(target.clone()) | var("b"));

let actual = input.derivative(target_set.clone());
assert!(!actual.inputs().contains(&target.clone()));

let evaluated_with_true = actual.evaluate(&btreeset_to_valuation(target_set.clone(), true));
let evaluated_with_false = actual.evaluate(&btreeset_to_valuation(target_set, false));

assert_eq!(evaluated_with_true, evaluated_with_false);
assert!(evaluated_with_true)
}

#[test]
fn test_derivative_xor_ok() {
let target = "a".to_string();
let target_set = BTreeSet::from([target.clone()]);
let input = TruthTable::from(var(target.clone()) ^ var("b"));

let actual = input.derivative(target_set.clone());
assert!(!actual.inputs().contains(&target.clone()));

let evaluated_with_true = actual.evaluate(&btreeset_to_valuation(target_set.clone(), true));
let evaluated_with_false = actual.evaluate(&btreeset_to_valuation(target_set, false));

assert_eq!(evaluated_with_true, evaluated_with_false);
assert!(evaluated_with_true)
}
}

0 comments on commit 5da60b4

Please sign in to comment.