Skip to content

Commit 3f33681

Browse files
committed
Improvements following feedback.
1 parent fcc94a9 commit 3f33681

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

nnf/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -998,51 +998,51 @@ def pair(node: NNF) -> NNF:
998998

999999
return pair(sentence)
10001000

1001-
def project(self: T_NNF, names: 't.FrozenSet[Name]') -> 'NNF':
1002-
"""Dual of :meth:``forget``: will forget all variables not given"""
1003-
return self.forget(self.vars() - names)
1001+
def project(self, names: 't.Iterable[Name]') -> 'NNF':
1002+
"""Dual of :meth:`forget: will forget all variables not given"""
1003+
return self.forget(self.vars() - frozenset(names))
10041004

1005-
def forget_aux(self: T_NNF) -> 'NNF':
1005+
def forget_aux(self) -> 'NNF':
10061006
"""Returns a theory that forgets all of the auxillary variables"""
1007-
aux_vars = frozenset({v for v in self.vars() if isinstance(v, Aux)})
1008-
return self.forget(aux_vars)
1007+
return self.forget(v for v in self.vars() if isinstance(v, Aux))
10091008

1010-
def forget(self: T_NNF, names: 't.FrozenSet[Name]') -> 'NNF':
1009+
def forget(self, names: 't.Iterable[Name]') -> 'NNF':
10111010
"""Forget a set of variables from the theory.
10121011
10131012
Has the effect of returning a theory without the variables provided,
10141013
such that every model of the new theory has an extension (i.e., an
10151014
assignment) to the forgotten variables that is a model of the original
10161015
theory.
10171016
1018-
:param names: A frozenset of the variable names to be forgotten
1017+
:param names: An iterable of the variable names to be forgotten
10191018
"""
10201019

10211020
if self.decomposable():
10221021
return self._forget_with_subs(names)
10231022
else:
10241023
return self._forget_with_shannon(names)
10251024

1026-
def _forget_with_subs(self: T_NNF, names: 't.FrozenSet[Name]') -> 'NNF':
1025+
def _forget_with_subs(self, names: 't.Iterable[Name]') -> 'NNF':
1026+
1027+
names = frozenset(names)
10271028

10281029
@memoize
10291030
def forget_recurse(node: NNF) -> NNF:
10301031
if isinstance(node, Var) and node.name in names:
10311032
return true
10321033
elif isinstance(node, Var):
10331034
return node
1034-
elif isinstance(node, And) or isinstance(node, Or):
1035-
new_children = map(forget_recurse, node.children)
1036-
return node.__class__(new_children)
1035+
elif isinstance(node, Internal):
1036+
return node.map(forget_recurse)
10371037
else:
10381038
raise TypeError(node)
10391039

10401040
return forget_recurse(self).simplify()
10411041

1042-
def _forget_with_shannon(self: T_NNF, vars: 't.FrozenSet[Name]') -> 'NNF':
1042+
def _forget_with_shannon(self, names: 't.Iterable[Name]') -> 'NNF':
10431043
T = self
1044-
for v in vars:
1045-
T = t.cast(T_NNF, T.condition({v: True}) | T.condition({v: False}))
1044+
for v in frozenset(names) & self.vars():
1045+
T = T.condition({v: True}) | T.condition({v: False})
10461046
return T.simplify()
10471047

10481048
def deduplicate(self: T_NNF) -> T_NNF:

test_nnf.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -735,20 +735,21 @@ def test_iff(a: nnf.NNF, b: nnf.NNF):
735735

736736
@given(NNF())
737737
def test_forget(sentence: nnf.NNF):
738+
# Assumption to reduce the time in testing
739+
assume(sentence.size() <= 15)
740+
738741
# Test that forgetting a backbone variable doesn't change the theory
739742
T = sentence & Var('added_var')
740743
assert sentence.equivalent(T.forget({'added_var'}))
741744

742-
# Assumption to reduce the time in testing
743-
assume(sentence.size() <= 15)
744-
745745
# Test the tseitin projection
746746
assert sentence.equivalent(sentence.to_CNF().forget_aux())
747747

748748
# Test that models of a projected theory are consistent with the original
749-
assume(len(sentence.vars()) > 2)
750-
vars = list(sentence.vars())[:2]
751-
T = sentence.forget(vars)
749+
names = list(sentence.vars())[:2]
750+
T = sentence.forget(names)
751+
assert not any([v in T.vars() for v in names])
752+
752753
for m in T.models():
753754
assert sentence.condition(m).satisfiable()
754755

@@ -757,8 +758,8 @@ def test_forget(sentence: nnf.NNF):
757758
def test_project(sentence: nnf.NNF):
758759
# Test that we get the same as projecting and forgetting
759760
assume(len(sentence.vars()) > 3)
760-
vars1 = frozenset(list(sentence.vars())[:2])
761-
vars2 = frozenset(list(sentence.vars())[2:])
761+
vars1 = list(sentence.vars())[:2]
762+
vars2 = list(sentence.vars())[2:]
762763
assert sentence.forget(vars1).equivalent(sentence.project(vars2))
763764

764765

0 commit comments

Comments
 (0)