From 4447d800df71c44b70c2d9eca743d33f77d06bf4 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad <600878@stud.hvl.no> Date: Sun, 14 Jul 2024 19:31:45 +0200 Subject: [PATCH] =?UTF-8?q?Fixed=20a=20few=20bugs=20in=20absorption=20and?= =?UTF-8?q?=20doubled=20the=20code=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/expressions/simplify.rs | 51 ++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/expressions/simplify.rs b/src/expressions/simplify.rs index d09eae9..0849ad7 100644 --- a/src/expressions/simplify.rs +++ b/src/expressions/simplify.rs @@ -360,14 +360,53 @@ impl Expression { operator, right, } if operator.is_or() - && (right.is_in(left) && (left.is_and() || left.is_or()) - || left.is_in(right) && right.is_or()) => + && (right.is_in(left) && left.is_and() || left.is_in(right) && right.is_or()) => { if let Some(operation) = Operation::new(self, right, Law::AbsorptionLaw) { operations.push(operation); } right.absorption_law(operations, ignore_case) } + Expression::Binary { + left, + operator: BinaryOperator::And, + right, + } if left.is_and() && right.is_in(left) => { + if let Some(operation) = Operation::new(self, left, Law::AbsorptionLaw) { + operations.push(operation); + } + left.absorption_law(operations, ignore_case) + } + Expression::Binary { + left, + operator: BinaryOperator::And, + right, + } if right.is_and() && left.is_in(right) => { + if let Some(operation) = Operation::new(self, right, Law::AbsorptionLaw) { + operations.push(operation); + } + right.absorption_law(operations, ignore_case) + } + Expression::Binary { + left, + operator: BinaryOperator::Or, + right, + } if left.is_or() && right.is_in(left) => { + if let Some(operation) = Operation::new(self, left, Law::AbsorptionLaw) { + operations.push(operation); + } + left.absorption_law(operations, ignore_case) + } + Expression::Binary { + left, + operator: BinaryOperator::Or, + right, + } if right.is_or() && left.is_in(right) => { + if let Some(operation) = Operation::new(self, right, Law::AbsorptionLaw) { + operations.push(operation); + } + right.absorption_law(operations, ignore_case) + } Expression::Binary { left, operator: BinaryOperator::Or, @@ -804,18 +843,16 @@ mod tests { // A ⋁ ¬B ⋁ A <=> ¬B ⋁ A #[test] fn test_absorption_law_duplicate_a_or_not_b_flipped() { - // TODO works on master let mut operations = vec![]; let expression = atomic("a") .or(not(atomic("b"))) .or(atomic("a")) - .absorption_law(&mut operations, Default::default()) - .absorb_opposites(&mut operations, Default::default()); - assert_eq!(expression, or(not(atomic("b")), atomic("a"))); + .absorption_law(&mut operations, Default::default()); + assert_eq!(expression, or(atomic("a"), not(atomic("b")))); assert_eq!(operations.len(), 1); assert_eq!(operations[0].law, Law::AbsorptionLaw); assert_eq!(operations[0].before, "a ⋁ ¬b ⋁ a"); - assert_eq!(operations[0].after, "¬b ⋁ a"); + assert_eq!(operations[0].after, "a ⋁ ¬b"); } #[test]