Skip to content

Commit 40e2033

Browse files
author
beckydvn
committed
optionally remove nesting when operator is used
1 parent 627bcec commit 40e2033

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

nnf/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,27 @@ def all_models(names: 't.Iterable[Name]') -> t.Iterator[Model]:
8484
new[name] = True
8585
yield new
8686

87+
auto_simplify = False
8788

8889
class NNF(metaclass=abc.ABCMeta):
8990
"""Base class for all NNF sentences."""
9091
__slots__ = ("__weakref__",)
91-
92+
93+
9294
def __and__(self: T_NNF, other: U_NNF) -> 'And[t.Union[T_NNF, U_NNF]]':
93-
"""And({self, other})"""
95+
"""And({self, other})"""
96+
# prevent unnecessary nesting
97+
if auto_simplify:
98+
if type(self) == And:
99+
return And({*self.children, *other.children}) if type(other) == And else And({*self.children, other})
94100
return And({self, other})
95101

96102
def __or__(self: T_NNF, other: U_NNF) -> 'Or[t.Union[T_NNF, U_NNF]]':
97103
"""Or({self, other})"""
104+
# prevent unnecessary nesting
105+
if auto_simplify:
106+
if type(self) == Or:
107+
return Or({*self.children, *other.children}) if type(other) == Or else Or({*self.children, other})
98108
return Or({self, other})
99109

100110
def __rshift__(self: T_NNF, other: U_NNF)-> 'Or[t.Union[T_NNF.negate(), U_NNF]]':

0 commit comments

Comments
 (0)