def test_commutativity(self): self.assertTrue( pcabuilder.InitProp('A and B').commutativity() == pcaprop. ConjunctionOp(pcaprop.Variable('B'), pcaprop.Variable('A'))) self.assertTrue( pcabuilder.InitProp('A and (C and B)').commutativity() == pcaprop.ConjunctionOp( pcaprop.ConjunctionOp(pcaprop.Variable( 'B'), pcaprop.Variable('C')), pcaprop.Variable('A'))) self.assertTrue( pcabuilder.InitProp('A or (C and ( D or B))').commutativity() == pcaprop.DisjunctionOp( pcaprop.ConjunctionOp( pcaprop.DisjunctionOp(pcaprop.Variable('B'), pcaprop.Variable('D')), pcaprop.Variable('C')), pcaprop.Variable('A'))) self.assertTrue( pcabuilder.InitProp('B implies (A and B)').commutativity() == pcaprop.ImplicationOp( pcaprop.Variable('B'), pcaprop.ConjunctionOp(pcaprop.Variable('B'), pcaprop.Variable('A')))) self.assertTrue( pcabuilder.InitProp('not not (A and B)').commutativity() == pcaprop.NegationOp( pcaprop.NegationOp( pcaprop.ConjunctionOp(pcaprop.Variable('B'), pcaprop.Variable('A')))))
def test_de_morgan(self): self.assertEqual( list(pcabuilder.InitProp('not(A or B)').interpretations()), list(pcabuilder.InitProp('(not A) and (not B)').interpretations())) self.assertTrue( pcabuilder.InitProp( '(not(A and B)) ⇔ ((not A) or (not B))').tautology()) self.assertTrue( pcabuilder.InitProp( '(not(A or B)) ⇔ ((not A) and (not B))').tautology()) self.assertEqual( pcabuilder.InitProp('¬(A or B)').de_morgan(), pcaprop.NegationOp( pcaprop.NegationOp( pcaprop.ConjunctionOp( pcaprop.NegationOp(pcaprop.Variable('A')), pcaprop.NegationOp(pcaprop.Variable('B')))))) self.assertEqual( pcabuilder.InitProp('¬(A and B)').de_morgan(), pcaprop.NegationOp( pcaprop.NegationOp( pcaprop.DisjunctionOp( pcaprop.NegationOp(pcaprop.Variable('A')), pcaprop.NegationOp(pcaprop.Variable('B')))))) to_test = pcaprop.NegationOp( pcaprop.ConjunctionOp( pcaprop.NegationOp(pcaprop.Variable('A')), pcaprop.NegationOp( pcaprop.NegationOp( pcaprop.NegationOp( pcaprop.DisjunctionOp( pcaprop.NegationOp(pcaprop.Variable('A')), pcaprop.NegationOp(pcaprop.Variable('B')))))))) self.assertEqual( pcabuilder.InitProp('A or ¬(A and B)').de_morgan(), to_test)
def test_multiples(self): a = pcaparser.PARSER.parse('A and B and C or D or F') to_assert = pcaprop.DisjunctionOp(pcaprop.DisjunctionOp( pcaprop.ConjunctionOp(pcaprop.ConjunctionOp(pcaprop.Variable('A'), pcaprop.Variable('B')), pcaprop.Variable('C')), pcaprop.Variable('D')), pcaprop.Variable('F')) self.assertEqual(a, to_assert) a = pcaparser.PARSER.parse('A implies B implies C iff D iff F') to_assert = pcaprop.EquivalenceOp(pcaprop.EquivalenceOp( pcaprop.ImplicationOp(pcaprop.ImplicationOp(pcaprop.Variable('A'), pcaprop.Variable('B')), pcaprop.Variable('C')), pcaprop.Variable('D')), pcaprop.Variable('F')) self.assertEqual(a, to_assert)
def test_prop(self): a = pcaparser.PARSER.parse('P or B iff C and A implies (P and C)') to_assert = pcaprop.EquivalenceOp(pcaprop.DisjunctionOp(pcaprop.Variable('P'), pcaprop.Variable('B')), pcaprop.ImplicationOp( pcaprop.ConjunctionOp(pcaprop.Variable('C'), pcaprop.Variable('A')), pcaprop.ConjunctionOp(pcaprop.Variable('P'), pcaprop.Variable('C')))) self.assertEqual(a, to_assert) a = pcaparser.PARSER.parse('true or not A implies false and not not not B or A') to_assert = pcaprop.ImplicationOp( pcaprop.DisjunctionOp(pcaprop.TrueProp(), pcaprop.NegationOp(pcaprop.Variable('A'))), pcaprop.DisjunctionOp( pcaprop.ConjunctionOp(pcaprop.FalseProp(), pcaprop.NegationOp( pcaprop.NegationOp(pcaprop.NegationOp(pcaprop.Variable('B'))))), pcaprop.Variable('A'))) self.assertEqual(a, to_assert)
def test_prop(self): a = pcaprop.TrueProp() b = pcaprop.NegationOp(a) self.assertTrue(a) self.assertFalse(b.eval()) self.assertTrue(pcaprop.DisjunctionOp(a, b.eval()).eval()) self.assertFalse(pcaprop.ConjunctionOp(b.eval(), a).eval())
def _de_morgan(op): if isinstance(op, (pcaprop.Variable, pcaprop.TrueProp, pcaprop.FalseProp)): return op if isinstance(op, pcaprop.UnaryOp): return op.__class__(_de_morgan(op.prop)) if isinstance(op, pcaprop.BinaryOp): prop_l = _de_morgan(op.prop_l) prop_r = _de_morgan(op.prop_r) if isinstance(op, pcaprop.DisjunctionOp): return pcaprop.NegationOp(pcaprop.ConjunctionOp(pcaprop.NegationOp(prop_l), pcaprop.NegationOp(prop_r))) if isinstance(op, pcaprop.ConjunctionOp): return pcaprop.NegationOp(pcaprop.DisjunctionOp(pcaprop.NegationOp(prop_l), pcaprop.NegationOp(prop_r))) return op.__class__(prop_l, prop_r)
def exp_and(self, value): if len(value) == 3: return pcaprop.ConjunctionOp(value[0], value[2]) return self._rec_object(value, 'exp_and')
def test_precedence(self): a = pcaprop.Variable("A") b = pcaprop.Variable("B") self.assertEqual((a + b >> ~a * b), pcaprop.ImplicationOp( pcaprop.DisjunctionOp(a, b), pcaprop.ConjunctionOp(pcaprop.NegationOp(a), b)))
def test_creation(self): a = pcaprop.Variable("A") b = pcaprop.Variable("B") c = pcaprop.Variable("C") self.assertEqual(a >> (~(a * b) + c), pcaprop.ImplicationOp( a, pcaprop.DisjunctionOp(pcaprop.NegationOp(pcaprop.ConjunctionOp(a, b)), c)))