|
| 1 | +use vortex_error::VortexResult; |
| 2 | + |
| 3 | +use super::nnf::nnf; |
| 4 | +use crate::traversal::{Node as _, NodeVisitor, TraversalOrder}; |
| 5 | +use crate::{BinaryExpr, ExprRef, Operator}; |
| 6 | + |
| 7 | +/// Return an equivalent expression in Conjunctive Normal Form (CNF). |
| 8 | +/// |
| 9 | +/// A CNF expression is a vector of vectors. The outer vector is a conjunction. The inner vectors |
| 10 | +/// are disjunctions. Neither [Operator::And] nor [Operator::Or] may appear in the |
| 11 | +/// disjunctions. Moreover, each disjunction in a CNF expression must be in Negative Normal Form. |
| 12 | +/// |
| 13 | +/// # Examples |
| 14 | +/// |
| 15 | +/// All the NNF examples also apply to CNF, for example double negation is removed entirely: |
| 16 | +/// |
| 17 | +/// ``` |
| 18 | +/// use vortex_expr::{not, col}; |
| 19 | +/// use vortex_expr::forms::cnf::cnf; |
| 20 | +/// |
| 21 | +/// let double_negation = not(not(col("a"))); |
| 22 | +/// let cnfed = cnf(double_negation).unwrap(); |
| 23 | +/// assert_eq!(cnfed, vec![vec![col("a")]]); |
| 24 | +/// ``` |
| 25 | +/// |
| 26 | +/// Unlike NNF, CNF, lifts conjunctions to the top-level and distributions disjunctions such that |
| 27 | +/// there is at most one disjunction for each conjunction operand: |
| 28 | +/// |
| 29 | +/// ``` |
| 30 | +/// use vortex_expr::{not, col, or, and}; |
| 31 | +/// use vortex_expr::forms::cnf::cnf; |
| 32 | +/// |
| 33 | +/// assert_eq!( |
| 34 | +/// cnf( |
| 35 | +/// or( |
| 36 | +/// or( |
| 37 | +/// and(col("a"), col("b")), |
| 38 | +/// col("c") |
| 39 | +/// ), |
| 40 | +/// col("d") |
| 41 | +/// ) |
| 42 | +/// ).unwrap(), |
| 43 | +/// vec![ |
| 44 | +/// vec![col("a"), col("c"), col("d")], |
| 45 | +/// vec![col("b"), col("c"), col("d")] |
| 46 | +/// ] |
| 47 | +/// ); |
| 48 | +/// ``` |
| 49 | +/// |
| 50 | +/// ``` |
| 51 | +/// use vortex_expr::{not, col, or, and}; |
| 52 | +/// use vortex_expr::forms::cnf::cnf; |
| 53 | +/// |
| 54 | +/// assert_eq!( |
| 55 | +/// cnf( |
| 56 | +/// or( |
| 57 | +/// and(col("a"), col("b")), |
| 58 | +/// col("c"), |
| 59 | +/// ) |
| 60 | +/// ).unwrap(), |
| 61 | +/// vec![ |
| 62 | +/// vec![col("a"), col("c")], |
| 63 | +/// vec![col("b"), col("c")], |
| 64 | +/// ] |
| 65 | +/// ); |
| 66 | +/// ``` |
| 67 | +/// |
| 68 | +/// Vortex extends the CNF definition to any Boolean-valued expression, even ones with non-Boolean |
| 69 | +/// parameters: |
| 70 | +/// |
| 71 | +/// ``` |
| 72 | +/// use vortex_expr::{not, col, or, and, gt_eq, lit, not_eq, lt, eq}; |
| 73 | +/// use vortex_expr::forms::cnf::cnf; |
| 74 | +/// use itertools::Itertools; |
| 75 | +/// |
| 76 | +/// assert_eq!( |
| 77 | +/// cnf( |
| 78 | +/// or( |
| 79 | +/// and( |
| 80 | +/// gt_eq(col("earnings"), lit(50_000)), |
| 81 | +/// not_eq(col("role"), lit("Manager")) |
| 82 | +/// ), |
| 83 | +/// col("special_flag") |
| 84 | +/// ), |
| 85 | +/// ).unwrap(), |
| 86 | +/// vec![ |
| 87 | +/// vec![ |
| 88 | +/// gt_eq(col("earnings"), lit(50_000)), |
| 89 | +/// col("special_flag") |
| 90 | +/// ], |
| 91 | +/// vec![ |
| 92 | +/// not_eq(col("role"), lit("Manager")), |
| 93 | +/// col("special_flag") |
| 94 | +/// ] |
| 95 | +/// ] |
| 96 | +/// ); |
| 97 | +/// ``` |
| 98 | +/// |
| 99 | +/// ``` |
| 100 | +/// use vortex_expr::{not, col, or, and, gt_eq, lit, not_eq, lt, eq}; |
| 101 | +/// use vortex_expr::forms::cnf::cnf; |
| 102 | +/// use itertools::Itertools; |
| 103 | +/// |
| 104 | +/// assert_eq!( |
| 105 | +/// cnf( |
| 106 | +/// or( |
| 107 | +/// or( |
| 108 | +/// and( |
| 109 | +/// gt_eq(col("earnings"), lit(50_000)), |
| 110 | +/// not_eq(col("role"), lit("Manager")) |
| 111 | +/// ), |
| 112 | +/// col("special_flag") |
| 113 | +/// ), |
| 114 | +/// and( |
| 115 | +/// lt(col("tenure"), lit(5)), |
| 116 | +/// eq(col("role"), lit("Engineer")) |
| 117 | +/// ), |
| 118 | +/// ) |
| 119 | +/// ).unwrap(), |
| 120 | +/// vec![ |
| 121 | +/// vec![ |
| 122 | +/// gt_eq(col("earnings"), lit(50_000)), |
| 123 | +/// col("special_flag"), |
| 124 | +/// lt(col("tenure"), lit(5)), |
| 125 | +/// ], |
| 126 | +/// vec![ |
| 127 | +/// gt_eq(col("earnings"), lit(50_000)), |
| 128 | +/// col("special_flag"), |
| 129 | +/// eq(col("role"), lit("Engineer")), |
| 130 | +/// ], |
| 131 | +/// vec![ |
| 132 | +/// not_eq(col("role"), lit("Manager")), |
| 133 | +/// col("special_flag"), |
| 134 | +/// lt(col("tenure"), lit(5)), |
| 135 | +/// ], |
| 136 | +/// vec![ |
| 137 | +/// not_eq(col("role"), lit("Manager")), |
| 138 | +/// col("special_flag"), |
| 139 | +/// eq(col("role"), lit("Engineer")), |
| 140 | +/// ], |
| 141 | +/// ] |
| 142 | +/// ); |
| 143 | +/// ``` |
| 144 | +/// |
| 145 | +pub fn cnf(expr: ExprRef) -> VortexResult<Vec<Vec<ExprRef>>> { |
| 146 | + let nnf = nnf(expr)?; |
| 147 | + |
| 148 | + let mut visitor = CNFVisitor::default(); |
| 149 | + nnf.accept(&mut visitor)?; |
| 150 | + Ok(visitor.finish()) |
| 151 | +} |
| 152 | + |
| 153 | +#[derive(Default)] |
| 154 | +struct CNFVisitor { |
| 155 | + conjuncts_of_disjuncts: Vec<Vec<ExprRef>>, |
| 156 | +} |
| 157 | + |
| 158 | +impl CNFVisitor { |
| 159 | + fn finish(self) -> Vec<Vec<ExprRef>> { |
| 160 | + self.conjuncts_of_disjuncts |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl NodeVisitor<'_> for CNFVisitor { |
| 165 | + type NodeTy = ExprRef; |
| 166 | + |
| 167 | + fn visit_down(&mut self, node: &ExprRef) -> VortexResult<TraversalOrder> { |
| 168 | + if let Some(binary_expr) = node.as_any().downcast_ref::<BinaryExpr>() { |
| 169 | + match binary_expr.op() { |
| 170 | + Operator::And => return Ok(TraversalOrder::Continue), |
| 171 | + Operator::Or => { |
| 172 | + let mut visitor = CNFVisitor::default(); |
| 173 | + binary_expr.lhs().accept(&mut visitor)?; |
| 174 | + let lhs_conjuncts = visitor.finish(); |
| 175 | + |
| 176 | + let mut visitor = CNFVisitor::default(); |
| 177 | + binary_expr.rhs().accept(&mut visitor)?; |
| 178 | + let rhs_conjuncts = visitor.finish(); |
| 179 | + |
| 180 | + self.conjuncts_of_disjuncts |
| 181 | + .extend(lhs_conjuncts.iter().flat_map(|lhs_disjunct| { |
| 182 | + rhs_conjuncts.iter().map(|rhs_disjunct| { |
| 183 | + let mut lhs_copy = lhs_disjunct.clone(); |
| 184 | + lhs_copy.extend(rhs_disjunct.iter().cloned()); |
| 185 | + lhs_copy |
| 186 | + }) |
| 187 | + })); |
| 188 | + |
| 189 | + return Ok(TraversalOrder::Skip); |
| 190 | + } |
| 191 | + _ => {} |
| 192 | + } |
| 193 | + } |
| 194 | + // Anything other than And and Or are terminals from the perspective of CNF |
| 195 | + self.conjuncts_of_disjuncts.push(vec![node.clone()]); |
| 196 | + Ok(TraversalOrder::Skip) |
| 197 | + } |
| 198 | +} |
0 commit comments