4

There are a lot of LINQ-based implementations of the Composite Specification Pattern. I have not seen one that used Subsumption.

Are there any such examples that have been documented (blogs, etc.) or published as open source? I have an idea and proof of concept for how this could work by having an ExpressionVisitor translate every specification into a canonical logical form (CNF/DNF), but I am concerned that this is overly complicated. Is there a better way?

smartcaveman
  • 41,281
  • 29
  • 127
  • 212

1 Answers1

2

I am concerned that this is overly complicated. Is there a better way?

The short answer is "No, there isn't" 1

The long answer: The "overly complicated" captures the essence of the problem: it is NP-hard. Here is a short informal proof relying upon the fact that the satisfiability problem is NP-complete:

  • Suppose that you have two Boolean formulas, A and B
  • You need to test if A implies B, or equivalently ¬A | B for all assignments of variables upon which A and B depend. In other words, you need a proof that F = ¬A | B is a tautology.
  • Suppose that the tautology test can be performed in polynomial time
  • Consider ¬F, the inverse of F. F is satisfiable if and only if ¬F is not a tautology
  • Use the hypothetical polynomial algorithm to test ¬F for being a tautology
  • The answer to "is F satisfiable" is the inverse of the answer to "is ¬F a tautology"
  • Therefore, an existence of a polynomial tautology checker would imply that the satisfiability problem is in P, and that P=NP.

Of course the fact that the problem is NP-hard does not mean that there would be no solutions for practical cases: in fact, your approach with the conversion to a canonical form may produce OK results in many real-world situations. However, an absence of a known "good" algorithm often discourages active development of practical solutions2.


1 With the obligatory "unless P=NP" disclaimer.

2 Unless a "reasonably good" solution would do, which may very well be the case for your problem, if you allow for "false negatives".

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • +1, Question: are you using tautology in the formal logic sense of the word or is there a similar but differently qualified definition in CS? Its my understanding that a proposition is tautologous iff it can be derived from the empty set of SC sentences. This would not adequately describe what I am trying to prove, because I have an extensive knowledge-base/database to derive from as well. On another note, the problem is difficult, but has been solved efficiently before.. Right now, I'm looking into Prolog, Euler and Microsoft Solver Foundation. – smartcaveman Dec 13 '12 at 04:22
  • @smartcaveman I am using tautology in the formal logic sense of the word: to prove that expression `A` subsumes `B`, you need to show that `A => B` is always true, i.e. can be derived from the empty set of premises. The only reason I mentioned it was to get to the satisfiability problem, which is known to be NP-complete. Note that only `A => B` must be a tautology; the individual expressions `A` and `B` can be, and in all non-trivial cases, will be, based on non-empty sets of premises. The bigger that set is, the harder is to prove the subsumption of `B` by `A`. – Sergey Kalinichenko Dec 13 '12 at 04:43
  • so I had another idea as to how to address this more practically - still working it out. Instead of a pure rewriting, I could create a square-of-opposition model for each predicate and require that all my predicates are in syllogistic form. This seems like it might mitigate the complexity, b/c the content would be normalized rather than the formal representation - since my domain is a business application, I think it could be expressive enough. Any thoughts? – smartcaveman Dec 14 '12 at 11:39
  • @smartcaveman Changing the representation would not change the fundamental hardness of the problem. Even if you normalize everything to 3-CNF, you're still in the NP space. Essentially, you are building a first-order theorem prover - it's not easy, unless you significantly restrict your predicates. Allowing false negatives in a few corner cases could be another tradeoff, if your application can tolerate it. – Sergey Kalinichenko Dec 14 '12 at 15:29
  • can you elaborate more about what you had in mind re: allowing false negatives for corner cases, or link me to somewhere that discusses this variation? – smartcaveman Dec 28 '12 at 14:23
  • @smartcaveman I meant trying a symbolic approach, where two expression trees are checked for [subgraph isomorphism](http://en.wikipedia.org/wiki/Subgraph_isomorphism_problem). Since trees are *planar graphs*, you can adopt a [linear-time algorithm](http://jgaa.info/accepted/99/Eppstein99.3.3.pdf) to find a match. Unfortunately, finding such matches would place undue restrictions on the representation of your expressions. As the result, it will be possible to construct a pair of expressions that contain a match, but the symbolic algorithm would not find it. – Sergey Kalinichenko Dec 28 '12 at 14:55
  • @smartcaveman For example, a simplistic symbolic approach would not find a match between `¬B & ¬C` and `A | ¬(B | C)`, because it does not know about DeMorgan's laws. You can certainly make lots of improvements to eliminate simple cases like this, but you wouldn't be able to cover them all. – Sergey Kalinichenko Dec 28 '12 at 14:59
  • thank you for clearing that up - I see what you mean now about the false negatives. I don't think I'm okay with making that compromise, however. I wouldn't consider DeMorgan's laws to be a "corner case"... am I mistaken in thinking that a lexical comparison of alphabetically sorted expression symbols in CNF would catch all cases? – smartcaveman Dec 29 '12 at 03:49
  • @smartcaveman If both expressions are CNFs, you no longer need DeMorgan's laws. You don't even need to sort, just run a subgraph isomorphism algorithm on the two expression trees. It's getting to CNF that's going to be tricky if you take this approach. – Sergey Kalinichenko Dec 29 '12 at 03:58