Skip to content

Commit

Permalink
Adding option to avoid simplification in conversion to CNF.
Browse files Browse the repository at this point in the history
  • Loading branch information
haz committed Dec 9, 2022
1 parent c2e81fd commit e8dee23
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions nnf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ def neg(node: NNF) -> NNF:

return neg(self)

def to_CNF(self) -> 'And[Or[Var]]':
def to_CNF(self, simplify_tautologies=True) -> 'And[Or[Var]]':
"""Compile theory to a semantically equivalent CNF formula."""
return tseitin.to_CNF(self)
return tseitin.to_CNF(self, simplify_tautologies)

def _cnf_satisfiable(self) -> bool:
"""Call a SAT solver on the presumed CNF theory."""
Expand Down
4 changes: 2 additions & 2 deletions nnf/tseitin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from nnf.util import memoize


def to_CNF(theory: NNF) -> And[Or[Var]]:
def to_CNF(theory: NNF, simplify_tautologies=True) -> And[Or[Var]]:
"""Convert an NNF into CNF using the Tseitin Encoding.
:param theory: Theory to convert.
Expand Down Expand Up @@ -73,7 +73,7 @@ def process_required(node: NNF) -> None:

elif isinstance(node, Or):
children = {process_node(c) for c in node.children}
if any(~var in children for var in children):
if simplify_tautologies and any(~var in children for var in children):
return
clauses.append(Or(children))

Expand Down
15 changes: 15 additions & 0 deletions test_nnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,21 @@ def test_models(sentence: nnf.NNF):
assert model_set(real_models) == model_set(models)


def test_tautology_simplification():
x = Var("x")
T = x | ~x

T1 = T.to_CNF()
T2 = T.to_CNF(simplify_tautologies=False)

assert T1 == nnf.true
assert T1.is_CNF()

assert T2 != nnf.true
assert T2 == And({Or({~x, x})})
assert T2.is_CNF()


@config(auto_simplify=False)
def test_nesting():
a, b, c, d, e, f = Var("a"), Var("b"), Var("c"), Var("d"), \
Expand Down

0 comments on commit e8dee23

Please sign in to comment.