ready_set_boole/src/negation_normal_form.rs

77 lines
2.3 KiB
Rust

mod tests;
use crate::ast::Node;
// fn cancel_double_negation(ast: &mut Node<char>, parent: Option<&mut Node<char>>) -> Node<char> {
// match ast {
// Node::Unary { operator: _, operand } => {
// if let Some(node) = parent {
// } else {
// *ast = cancel_double_negation(&mut *operand, Some(ast));
// }
// },
// Node::Binary { operator: _, lhs, rhs } => {
// *ast = cancel_double_negation(&mut *lhs, None);
// *ast = cancel_double_negation(&mut *rhs, None);
// },
// Node::Leaf(c) => (),
// }
// ast
// }
// fn cancel_double_negation(ast: Node<char>) {
// let mut new_ast = ast.clone();
// fn recursion(ast: &mut Node<char>, node_count: usize) {
// match ast {
// Node::Unary { operator: _, operand } => {
// recursion(&mut *operand, node_count + 1);
// },
// Node::Binary { operator: _, lhs, rhs } => {
// recursion(&mut *lhs, node_count + 1);
// recursion(&mut *rhs, node_count + 1);
// },
// Node::Leaf(c) => (),
// }
// }
// recursion(&mut new_ast, 0);
// new_ast
// }
fn negation_normal_form(formula: &str) -> String {
let mut ast = Node::<char>::parse_formula(formula);
// let mut tmp = Node::<char>::parse_formula((formula.to_string() + "!").as_str());
// let mut clone = ast.clone();
// while Node::<char>::ast_to_formula(&clone) != Node::<char>::ast_to_formula(&tmp) {
// let copy = ast.clone();
// clone = ast.clone();
// dbg!(&ast);
// tmp = match copy {
// Node::Unary { operator: _, operand } => {
// match *operand {
// Node::Unary { operator: _, operand } => {
// println!("salut");
// dbg!(&*operand);
// *operand
// }
// Node::Binary { operator: _, lhs: _, rhs: _ } => ast,
// Node::Leaf(_) => ast,
// }
// }
// Node::Binary { operator: _, lhs: _, rhs: _ } => ast,
// Node::Leaf(_) => ast,
// };
// ast = tmp.clone();
// }
ast.simplify();
Node::<char>::ast_to_formula(&ast)
}