2024-06-14 18:38:37 +02:00
|
|
|
|
use std::ops::Deref;
|
2024-06-16 19:24:52 +02:00
|
|
|
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
2024-06-21 18:07:42 +02:00
|
|
|
|
use crate::expressions::expression::Expression;
|
2024-06-14 18:38:37 +02:00
|
|
|
|
use crate::expressions::helpers::{and, binary, not, or};
|
2024-06-05 20:41:00 +02:00
|
|
|
|
use crate::expressions::operator::BinaryOperator;
|
2024-06-21 18:07:42 +02:00
|
|
|
|
use crate::routing::options::SimplifyOptions;
|
2024-06-16 19:24:52 +02:00
|
|
|
|
use crate::routing::response::Operation;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize)]
|
|
|
|
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
2024-06-25 19:57:23 +02:00
|
|
|
|
#[allow(clippy::enum_variant_names)]
|
2024-06-16 19:24:52 +02:00
|
|
|
|
pub enum Law {
|
|
|
|
|
EliminationOfImplication,
|
|
|
|
|
DeMorgansLaws,
|
|
|
|
|
AbsorptionLaw,
|
|
|
|
|
AssociativeLaw,
|
|
|
|
|
DistributionLaw,
|
|
|
|
|
DoubleNegationElimination,
|
|
|
|
|
CommutativeLaw,
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 23:37:26 +02:00
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! absorption_law_opposites {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
($left:expr, $right:expr, $operations:expr, $op:pat, $func:expr, $ignore_case:expr) => {
|
2024-06-20 23:37:26 +02:00
|
|
|
|
match ($left.as_ref(), $right.as_ref()) {
|
|
|
|
|
(_, Expression::Binary { left: right_left, operator: $op, right: right_right }) => {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
evaluate_equals_or_opposites($left.as_ref(), right_left, right_right, $func, $ignore_case, $operations).unwrap_or(
|
|
|
|
|
$func($left.absorption_law($operations, $ignore_case), $right.absorption_law($operations, $ignore_case))
|
2024-06-20 23:37:26 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
(Expression::Binary { left: left_left, operator: $op, right: left_right }, _) => {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
evaluate_equals_or_opposites($right.as_ref(), left_left, left_right, $func, $ignore_case, $operations).unwrap_or(
|
|
|
|
|
$func($left.absorption_law($operations, $ignore_case), $right.absorption_law($operations, $ignore_case))
|
2024-06-20 23:37:26 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
2024-06-21 18:07:42 +02:00
|
|
|
|
(left, right) => $func(left.absorption_law($operations, $ignore_case), right.absorption_law($operations, $ignore_case))
|
2024-06-20 23:37:26 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! distribution_law_atomic_vs_binary {
|
|
|
|
|
($left:expr, $right:expr, $operations:expr, $op:pat, $func1:expr, $func2:expr) => {
|
|
|
|
|
match ($left.as_ref(), $right.as_ref()) {
|
|
|
|
|
(Expression::Atomic(_), Expression::Binary { left: right_left, operator: $op, right: right_right }) => {
|
|
|
|
|
let right_left = right_left.distribution_law($operations);
|
|
|
|
|
let right_right = right_right.distribution_law($operations);
|
|
|
|
|
$func1($func2($left.clone(), right_left), $func2($left.clone(), right_right))
|
|
|
|
|
}
|
|
|
|
|
(Expression::Binary { left: left_left, operator: $op, right: left_right }, Expression::Atomic(_)) => {
|
|
|
|
|
let left_left = left_left.distribution_law($operations);
|
|
|
|
|
let left_right = left_right.distribution_law($operations);
|
|
|
|
|
$func1($func2(left_left, $right.clone()), $func2(left_right, $right.clone()))
|
|
|
|
|
}
|
|
|
|
|
(left, right) => $func2(left.distribution_law($operations), right.distribution_law($operations))
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 18:07:42 +02:00
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub struct Options {
|
|
|
|
|
pub ignore_case: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<SimplifyOptions> for Options {
|
|
|
|
|
fn from(options: SimplifyOptions) -> Self {
|
|
|
|
|
Self { ignore_case: options.ignore_case }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:24:52 +02:00
|
|
|
|
impl Expression {
|
|
|
|
|
// TODO better track of operations
|
2024-06-21 18:07:42 +02:00
|
|
|
|
pub fn simplify(&self, options: Options) -> (Self, Vec<Operation>) {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations: Vec<Operation> = vec![];
|
|
|
|
|
let expression = self.elimination_of_implication(&mut operations)
|
|
|
|
|
.de_morgans_laws(&mut operations)
|
2024-06-21 18:07:42 +02:00
|
|
|
|
.absorption_law(&mut operations, options.ignore_case)
|
2024-06-16 19:24:52 +02:00
|
|
|
|
// .associative_law(&mut operations)
|
|
|
|
|
.distribution_law(&mut operations)
|
|
|
|
|
.double_negation_elimination(&mut operations);
|
|
|
|
|
// .commutative_law(&mut operations);
|
|
|
|
|
(expression, operations)
|
2024-06-06 23:53:30 +02:00
|
|
|
|
}
|
2024-06-16 19:24:52 +02:00
|
|
|
|
|
2024-06-05 20:41:00 +02:00
|
|
|
|
/// Eliminate the implication operator from the expression.
|
|
|
|
|
/// This is done by replacing `a ➔ b` with `¬a ⋁ b`.
|
2024-06-16 19:24:52 +02:00
|
|
|
|
fn elimination_of_implication(&self, operations: &mut Vec<Operation>) -> Self {
|
2024-06-25 19:57:23 +02:00
|
|
|
|
match self {
|
|
|
|
|
Expression::Not(expr) => {
|
|
|
|
|
not(expr.elimination_of_implication(operations))
|
|
|
|
|
}
|
2024-06-07 16:27:52 +02:00
|
|
|
|
Expression::Binary { left, operator, right } => {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let l_result = left.elimination_of_implication(operations);
|
|
|
|
|
let r_result = right.elimination_of_implication(operations);
|
2024-06-25 19:57:23 +02:00
|
|
|
|
let before = binary(l_result.clone(), *operator, r_result.clone());
|
2024-06-16 19:24:52 +02:00
|
|
|
|
|
|
|
|
|
if let BinaryOperator::Implication = *operator {
|
2024-06-25 19:57:23 +02:00
|
|
|
|
let after = or(not(l_result.clone()), r_result.clone());
|
|
|
|
|
if let Some(operation) = Operation::new(&before, &after, Law::EliminationOfImplication) {
|
|
|
|
|
operations.push(operation);
|
|
|
|
|
}
|
|
|
|
|
after
|
2024-06-16 19:24:52 +02:00
|
|
|
|
} else {
|
2024-06-25 19:57:23 +02:00
|
|
|
|
before
|
2024-06-16 19:24:52 +02:00
|
|
|
|
}
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
atomic @ Expression::Atomic(_) => atomic.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Eliminate double negations from the expression.
|
|
|
|
|
/// This is done by replacing `¬¬a` with `a`.
|
|
|
|
|
/// This function is recursive and will continue to eliminate double negations until none are left.
|
2024-06-16 19:24:52 +02:00
|
|
|
|
fn double_negation_elimination(&self, operations: &mut Vec<Operation>) -> Self {
|
|
|
|
|
let result = match self {
|
2024-06-05 20:41:00 +02:00
|
|
|
|
Expression::Not(expr) => {
|
2024-06-25 19:57:23 +02:00
|
|
|
|
let before = not(expr.double_negation_elimination(operations));
|
2024-06-14 18:38:37 +02:00
|
|
|
|
if let Expression::Not(inner) = expr.deref() {
|
2024-06-25 19:57:23 +02:00
|
|
|
|
let after = inner.double_negation_elimination(operations);
|
|
|
|
|
dbg!(before.to_string(), after.to_string());
|
|
|
|
|
if let Some(operation) = Operation::new(&before, &after, Law::DoubleNegationElimination) {
|
|
|
|
|
operations.push(operation);
|
|
|
|
|
}
|
|
|
|
|
after
|
2024-06-05 20:41:00 +02:00
|
|
|
|
} else {
|
2024-06-25 19:57:23 +02:00
|
|
|
|
before
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-07 16:27:52 +02:00
|
|
|
|
Expression::Binary { left, operator, right } => {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let left = left.double_negation_elimination(operations);
|
|
|
|
|
let right = right.double_negation_elimination(operations);
|
|
|
|
|
binary(left.clone(), *operator, right.clone())
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
atomic @ Expression::Atomic(_) => atomic.clone(),
|
2024-06-16 19:24:52 +02:00
|
|
|
|
};
|
2024-06-25 19:57:23 +02:00
|
|
|
|
// if let Some(operation) = Operation::new(self, &result, Law::DoubleNegationElimination) {
|
|
|
|
|
// operations.push(operation);
|
|
|
|
|
// }
|
2024-06-16 19:24:52 +02:00
|
|
|
|
result
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:24:52 +02:00
|
|
|
|
fn de_morgans_laws(&self, operations: &mut Vec<Operation>) -> Self {
|
|
|
|
|
let result = match self {
|
2024-06-05 20:41:00 +02:00
|
|
|
|
Expression::Not(expr) => {
|
2024-06-14 18:38:37 +02:00
|
|
|
|
match expr.deref() {
|
2024-06-20 23:37:26 +02:00
|
|
|
|
Expression::Binary { left, operator: operator @ (BinaryOperator::And | BinaryOperator::Or), right } => {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let left = not(left.de_morgans_laws(operations));
|
|
|
|
|
let right = not(right.de_morgans_laws(operations));
|
2024-06-20 23:37:26 +02:00
|
|
|
|
if let BinaryOperator::And = operator {
|
|
|
|
|
or(left, right)
|
|
|
|
|
} else {
|
|
|
|
|
and(left, right)
|
|
|
|
|
}.de_morgans_laws(operations)
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-16 19:24:52 +02:00
|
|
|
|
_ => not(expr.de_morgans_laws(operations)),
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-07 16:27:52 +02:00
|
|
|
|
Expression::Binary { left, operator, right } => {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let left = left.de_morgans_laws(operations);
|
|
|
|
|
let right = right.de_morgans_laws(operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
binary(left, *operator, right)
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
atomic @ Expression::Atomic(_) => atomic.clone(),
|
2024-06-16 19:24:52 +02:00
|
|
|
|
};
|
|
|
|
|
if let Some(operation) = Operation::new(self, &result, Law::DeMorgansLaws) {
|
|
|
|
|
operations.push(operation);
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-16 19:24:52 +02:00
|
|
|
|
result
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 18:07:42 +02:00
|
|
|
|
fn absorption_law(&self, operations: &mut Vec<Operation>, ignore_case: bool) -> Self {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let result = match self {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
Expression::Binary { left, operator: BinaryOperator::And | BinaryOperator::Or, right }
|
|
|
|
|
if Expression::eq(left, right, ignore_case) => {
|
|
|
|
|
left.absorption_law(operations, ignore_case)
|
2024-06-16 19:24:52 +02:00
|
|
|
|
}
|
2024-06-07 16:27:52 +02:00
|
|
|
|
Expression::Binary { left, operator: BinaryOperator::And, right } => {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
absorption_law_opposites!(left, right, operations, BinaryOperator::Or, and, ignore_case)
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-07 16:27:52 +02:00
|
|
|
|
Expression::Binary { left, operator: BinaryOperator::Or, right } => {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
absorption_law_opposites!(left, right, operations, BinaryOperator::And, or, ignore_case)
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-20 23:37:26 +02:00
|
|
|
|
Expression::Binary { left, operator, right } => binary(
|
2024-06-21 18:07:42 +02:00
|
|
|
|
left.absorption_law(operations, ignore_case),
|
2024-06-20 23:37:26 +02:00
|
|
|
|
*operator,
|
2024-06-21 18:07:42 +02:00
|
|
|
|
right.absorption_law(operations, ignore_case),
|
2024-06-20 23:37:26 +02:00
|
|
|
|
),
|
2024-06-21 18:07:42 +02:00
|
|
|
|
Expression::Not(expr) => not(expr.absorption_law(operations, ignore_case)),
|
2024-06-05 20:41:00 +02:00
|
|
|
|
atomic => atomic.clone(),
|
2024-06-16 19:24:52 +02:00
|
|
|
|
};
|
|
|
|
|
if let Some(operation) = Operation::new(self, &result, Law::AbsorptionLaw) {
|
|
|
|
|
operations.push(operation);
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-16 19:24:52 +02:00
|
|
|
|
result
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:24:52 +02:00
|
|
|
|
fn associative_law(&self, operations: &mut Vec<Operation>) -> Self {
|
2024-06-05 20:41:00 +02:00
|
|
|
|
todo!("? | Associative law: (a ⋀ b) ⋀ c == a ⋀ (b ⋀ c) and (a ⋁ b) ⋁ c == a ⋁ (b ⋁ c)")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:24:52 +02:00
|
|
|
|
fn distribution_law(&self, operations: &mut Vec<Operation>) -> Self {
|
|
|
|
|
let result = match self {
|
2024-06-07 16:27:52 +02:00
|
|
|
|
Expression::Binary { left, operator: BinaryOperator::And, right } => {
|
2024-06-20 23:37:26 +02:00
|
|
|
|
distribution_law_atomic_vs_binary!(left, right, operations, BinaryOperator::Or, or, and)
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-07 16:27:52 +02:00
|
|
|
|
Expression::Binary { left, operator: BinaryOperator::Or, right } => {
|
2024-06-20 23:37:26 +02:00
|
|
|
|
distribution_law_atomic_vs_binary!(left, right, operations, BinaryOperator::And, and, or)
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-20 23:37:26 +02:00
|
|
|
|
Expression::Binary { left, operator, right } => binary(
|
|
|
|
|
left.distribution_law(operations),
|
|
|
|
|
*operator,
|
|
|
|
|
right.distribution_law(operations),
|
|
|
|
|
),
|
2024-06-16 19:24:52 +02:00
|
|
|
|
Expression::Not(expr) => not(expr.distribution_law(operations)),
|
2024-06-05 20:41:00 +02:00
|
|
|
|
atomic => atomic.clone(),
|
2024-06-16 19:24:52 +02:00
|
|
|
|
};
|
|
|
|
|
if let Some(operation) = Operation::new(self, &result, Law::DistributionLaw) {
|
|
|
|
|
operations.push(operation);
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-16 19:24:52 +02:00
|
|
|
|
result
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:24:52 +02:00
|
|
|
|
fn commutative_law(&self, operations: &mut Vec<Operation>) -> Self {
|
2024-06-05 20:41:00 +02:00
|
|
|
|
todo!("? | Order of operands does not matter in AND and OR operations.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:24:52 +02:00
|
|
|
|
fn evaluate_equals_or_opposites<F: Fn(Expression, Expression) -> Expression>(
|
|
|
|
|
this: &Expression,
|
|
|
|
|
left: &Expression,
|
|
|
|
|
right: &Expression,
|
|
|
|
|
ret_func: F,
|
2024-06-21 18:07:42 +02:00
|
|
|
|
ignore_case: bool,
|
2024-06-16 19:24:52 +02:00
|
|
|
|
operations: &mut Vec<Operation>,
|
|
|
|
|
) -> Option<Expression> {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
if this.eq(left, ignore_case) || this.eq(right, ignore_case) {
|
|
|
|
|
return Some(this.absorption_law(operations, ignore_case));
|
|
|
|
|
} else if left.is_atomic() && right.is_atomic() && this.opposite_eq(left, ignore_case) {
|
|
|
|
|
if this.opposite_eq(left, ignore_case) {
|
|
|
|
|
return Some(ret_func(right.absorption_law(operations, ignore_case), this.absorption_law(operations, ignore_case)));
|
|
|
|
|
} else if this.opposite_eq(right, ignore_case) {
|
|
|
|
|
return Some(ret_func(left.absorption_law(operations, ignore_case), this.absorption_law(operations, ignore_case)));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 20:41:00 +02:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2024-06-14 18:38:37 +02:00
|
|
|
|
use crate::expressions::helpers::{and, atomic, implies, not, or};
|
2024-06-16 19:24:52 +02:00
|
|
|
|
use crate::expressions::simplify::Law;
|
2024-06-05 20:41:00 +02:00
|
|
|
|
|
2024-06-06 23:53:30 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_simplify() {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let (expression, operations) = implies(atomic("a"), atomic("b")).simplify(Default::default());
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(not(atomic("a")), atomic("b")));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::EliminationOfImplication);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_simplify_a_and_a() {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let (expression, operations) = and(atomic("a"), atomic("a")).simplify(Default::default());
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(expression, atomic("a"));
|
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::AbsorptionLaw);
|
2024-06-06 23:53:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_implication_and_de_morgans() {
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = implies(and(not(atomic("a")), atomic("b")), atomic("c")).simplify(Default::default()).0;
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(or(atomic("a"), not(atomic("b"))), atomic("c")));
|
2024-06-06 23:53:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 20:41:00 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_elimination_of_implication() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = implies(atomic("a"), atomic("b")).elimination_of_implication(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(not(atomic("a")), atomic("b")));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::EliminationOfImplication);
|
|
|
|
|
assert_eq!(operations[0].before, "a ➔ b");
|
2024-06-17 11:41:28 +02:00
|
|
|
|
assert_eq!(operations[0].after, "¬a ⋁ b");
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_elimination_of_implication_nested() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = implies(atomic("a"), implies(atomic("b"), atomic("c"))).elimination_of_implication(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(not(atomic("a")), or(not(atomic("b")), atomic("c"))));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 2);
|
|
|
|
|
assert_eq!(operations[0].law, Law::EliminationOfImplication);
|
|
|
|
|
assert_eq!(operations[0].before, "b ➔ c");
|
2024-06-17 11:41:28 +02:00
|
|
|
|
assert_eq!(operations[0].after, "¬b ⋁ c");
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations[1].law, Law::EliminationOfImplication);
|
2024-06-25 19:57:23 +02:00
|
|
|
|
assert_eq!(operations[1].before, "a ➔ ¬b ⋁ c");
|
2024-06-17 11:41:28 +02:00
|
|
|
|
assert_eq!(operations[1].after, "¬a ⋁ ¬b ⋁ c");
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_elimination_of_implication_none() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = and(atomic("a"), atomic("b")).elimination_of_implication(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, and(atomic("a"), atomic("b")));
|
2024-06-25 19:57:23 +02:00
|
|
|
|
assert_eq!(operations.len(), 0);
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_elimination_of_implication_nested_none() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = or(atomic("a"), and(atomic("b"), atomic("c"))).elimination_of_implication(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(atomic("a"), and(atomic("b"), atomic("c"))));
|
2024-06-25 19:57:23 +02:00
|
|
|
|
assert_eq!(operations.len(), 0);
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_double_negation_elimination() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(not(atomic("a"))).double_negation_elimination(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, atomic("a"));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::DoubleNegationElimination);
|
|
|
|
|
assert_eq!(operations[0].before, "¬¬a");
|
|
|
|
|
assert_eq!(operations[0].after, "a");
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_triple_negation_elimination() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(not(not(atomic("a")))).double_negation_elimination(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, not(atomic("a")));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::DoubleNegationElimination);
|
|
|
|
|
assert_eq!(operations[0].before, "¬¬¬a");
|
|
|
|
|
assert_eq!(operations[0].after, "¬a");
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_five_negation_elimination() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(not(not(not(not(atomic("a")))))).double_negation_elimination(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, not(atomic("a")));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 2);
|
|
|
|
|
assert_eq!(operations[0].law, Law::DoubleNegationElimination);
|
|
|
|
|
assert_eq!(operations[0].before, "¬¬¬a");
|
|
|
|
|
assert_eq!(operations[0].after, "¬a");
|
|
|
|
|
assert_eq!(operations[1].law, Law::DoubleNegationElimination);
|
|
|
|
|
assert_eq!(operations[1].before, "¬¬¬¬¬a");
|
2024-06-25 19:57:23 +02:00
|
|
|
|
assert_eq!(operations[1].after, "¬¬¬a");
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_no_negation_elimination() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = atomic("a").double_negation_elimination(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, atomic("a"));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_double_negation_nested_elimination() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = and(or(not(not(atomic("a"))), atomic("b")), not(not(atomic("c")))).double_negation_elimination(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, and(or(atomic("a"), atomic("b")), atomic("c")));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 4);
|
|
|
|
|
assert!(operations.into_iter().map(|operation| operation.law).all(|law| law == Law::DoubleNegationElimination));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_de_morgans_laws_and() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(and(atomic("a"), atomic("b"))).de_morgans_laws(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(not(atomic("a")), not(atomic("b"))));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::DeMorgansLaws);
|
|
|
|
|
assert_eq!(operations[0].before, "¬(a ⋀ b)");
|
2024-06-17 11:41:28 +02:00
|
|
|
|
assert_eq!(operations[0].after, "¬a ⋁ ¬b");
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_de_morgans_laws_or() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(or(atomic("a"), atomic("b"))).de_morgans_laws(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, and(not(atomic("a")), not(atomic("b"))));
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::DeMorgansLaws);
|
|
|
|
|
assert_eq!(operations[0].before, "¬((a ⋁ b))");
|
|
|
|
|
assert_eq!(operations[0].after, "¬a ⋀ ¬b");
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_de_morgans_laws_nested_or() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(or(and(atomic("a"), atomic("b")), atomic("c"))).de_morgans_laws(&mut operations); // ¬(a ⋀ b ⋁ c)
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, and(or(not(atomic("a")), not(atomic("b"))), not(atomic("c")))); // ¬(a ⋀ b) ⋀ ¬c == (¬a ⋁ ¬b) ⋀ ¬c
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(operations.len(), 3);
|
|
|
|
|
assert!(operations.into_iter().map(|operation| operation.law).all(|law| law == Law::DeMorgansLaws));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_de_morgans_laws_nested_and() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(and(or(atomic("a"), atomic("b")), atomic("c"))).de_morgans_laws(&mut operations); // ¬(a ⋁ b ⋀ c)
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(and(not(atomic("a")), not(atomic("b"))), not(atomic("c")))); // ¬(a ⋁ b) ⋀ ¬c == (¬a ⋀ ¬b) ⋁ ¬c
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_de_morgans_laws_nested_and_or() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = not(and(or(atomic("a"), atomic("b")), or(atomic("c"), atomic("d")))).de_morgans_laws(&mut operations); // ¬(a ⋁ b ⋀ c ⋁ d)
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(and(not(atomic("a")), not(atomic("b"))), and(not(atomic("c")), not(atomic("d"))))); // ¬(a ⋁ b) ⋀ ¬(c ⋁ d) == (¬a ⋀ ¬b) ⋁ (¬c ⋀ ¬d)
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_absorption_law_and() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = and(atomic("a"), or(atomic("a"), atomic("b"))).absorption_law(&mut operations, false);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, atomic("a"));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_absorption_law_or() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = or(atomic("a"), and(atomic("a"), atomic("b"))).absorption_law(&mut operations, false);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, atomic("a"));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_absorption_law_nested_and() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = and(atomic("a"), or(atomic("a"), atomic("b"))).absorption_law(&mut operations, Default::default());
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, atomic("a"));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// !A & B | A <=> B | A
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_absorption_law_not() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = or(and(not(atomic("a")), atomic("b")), atomic("a")).absorption_law(&mut operations, Default::default());
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(atomic("b"), atomic("a")));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A & B | !A <=> B | !A
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_absorption_law_not_reversed() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = or(and(atomic("a"), atomic("b")), not(atomic("a"))).absorption_law(&mut operations, Default::default());
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(atomic("b"), not(atomic("a"))));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// !A & B | !A <=> !A
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_absorption_law_double_not() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = or(and(not(atomic("a")), atomic("b")), not(atomic("a"))).absorption_law(&mut operations, Default::default());
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, not(atomic("a")));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:24:52 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_absorption_law_duplicate_atomic() {
|
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = and(atomic("A"), atomic("A"));
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let simplified = expression.absorption_law(&mut operations, Default::default());
|
2024-06-16 19:24:52 +02:00
|
|
|
|
assert_eq!(simplified, atomic("A"));
|
|
|
|
|
assert_eq!(operations.len(), 1);
|
|
|
|
|
assert_eq!(operations[0].law, Law::AbsorptionLaw);
|
|
|
|
|
assert_eq!(operations[0].before, "A ⋀ A");
|
|
|
|
|
assert_eq!(operations[0].after, "A");
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 20:41:00 +02:00
|
|
|
|
// (A | B) & !A <=> B & !A
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_in_parenthesis() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
2024-06-21 18:07:42 +02:00
|
|
|
|
let expression = and(or(atomic("a"), atomic("b")), not(atomic("a"))).absorption_law(&mut operations, Default::default());
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, and(atomic("b"), not(atomic("a"))));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_distributive_law_and() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = and(atomic("a"), or(atomic("b"), atomic("c"))).distribution_law(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, or(and(atomic("a"), atomic("b")), and(atomic("a"), atomic("c"))));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_distributive_law_or() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = or(atomic("a"), and(atomic("b"), atomic("c"))).distribution_law(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, and(or(atomic("a"), atomic("b")), or(atomic("a"), atomic("c"))));
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|
2024-06-06 23:53:30 +02:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_distributive_law_nested_not() {
|
2024-06-16 19:24:52 +02:00
|
|
|
|
let mut operations = vec![];
|
|
|
|
|
let expression = and(atomic("a"), not(or(atomic("b"), atomic("c")))).distribution_law(&mut operations);
|
2024-06-14 18:38:37 +02:00
|
|
|
|
assert_eq!(expression, and(atomic("a"), not(or(atomic("b"), atomic("c")))))
|
2024-06-06 23:53:30 +02:00
|
|
|
|
}
|
2024-06-05 20:41:00 +02:00
|
|
|
|
}
|