Exemplo n.º 1
0
def _apply_methods(prop, methods):
    prop = pcabuilder.InitProp(prop)
    methods = [getattr(pcabuilder.InitProp, method) for method in methods.split(',')]
    for method in methods:
        prop = method(prop)
        prop = pcabuilder.InitProp(str(prop))
    return prop
Exemplo n.º 2
0
 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')))))
Exemplo n.º 3
0
 def test_unique_vars(self):
     to_test = [pcaprop.Variable('A'), pcaprop.Variable('B')]
     self.assertEqual(
         pcabuilder.InitProp('Bor A iff B and A').unique_vars(), to_test)
     self.assertEqual([
         variable.name for variable in pcabuilder.InitProp(
             'Bor A iff B and A').unique_vars()
     ], ['A', 'B'])
Exemplo n.º 4
0
 def test_involution(self):
     self.assertTrue(
         pcabuilder.InitProp('not not not not A').involution() ==
         pcaprop.Variable('A'))
     self.assertTrue(
         pcabuilder.InitProp('not not (B or not not A)').involution()
         == pcaprop.DisjunctionOp(pcaprop.Variable('B'),
                                  pcaprop.Variable('A')))
Exemplo n.º 5
0
 def test_simple_props(self):
     to_test = [[pcaprop.FalseProp(),
                 pcaprop.FalseProp()],
                [pcaprop.TrueProp(), pcaprop.TrueProp()]]
     self.assertEqual(list(pcabuilder.InitProp('A').interpretations()),
                      to_test)
     to_test = [[pcaprop.FalseProp(),
                 pcaprop.TrueProp()],
                [pcaprop.TrueProp(),
                 pcaprop.FalseProp()]]
     self.assertEqual(
         list(pcabuilder.InitProp('not (not (not A))').interpretations()),
         to_test)
Exemplo n.º 6
0
 def test_implication(self):
     self.assertTrue(
         pcabuilder.InitProp('A implies (B implies C) ').implication() ==
         pcaprop.DisjunctionOp(
             pcaprop.NegationOp(pcaprop.Variable('A')),
             pcaprop.DisjunctionOp(
                 pcaprop.NegationOp(pcaprop.Variable('B')),
                 pcaprop.Variable('C'))))
     self.assertTrue(
         pcabuilder.InitProp('A or not (A implies B)').implication() ==
         pcaprop.DisjunctionOp(
             pcaprop.Variable('A'),
             pcaprop.NegationOp(
                 pcaprop.DisjunctionOp(
                     pcaprop.NegationOp(pcaprop.Variable('A')),
                     pcaprop.Variable('B')))))
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 def test_minimum(self):
     self.assertTrue(
         pcabuilder.InitProp('A or false').minimum() == pcaprop.Variable(
             'A'))
     self.assertTrue(
         pcabuilder.InitProp('false or (A iff B)').minimum() == pcaprop.
         EquivalenceOp(pcaprop.Variable('A'), pcaprop.Variable('B')))
     self.assertTrue(
         pcabuilder.InitProp('A and false').minimum() ==
         pcaprop.FalseProp())
     self.assertTrue(
         pcabuilder.InitProp('false and (A iff B)').minimum() ==
         pcaprop.FalseProp())
     self.assertTrue(
         pcabuilder.InitProp(
             'B iff (A and (false and (A iff B)))').minimum() ==
         pcaprop.EquivalenceOp(pcaprop.Variable('B'), pcaprop.FalseProp()))
     self.assertTrue(
         pcabuilder.InitProp('Z iff (A and (B and bot))').minimum() ==
         pcaprop.EquivalenceOp(pcaprop.Variable('Z'), pcaprop.FalseProp()))
     self.assertTrue(
         pcabuilder.InitProp('Z iff (not A or not(B or bot))').minimum() ==
         pcaprop.EquivalenceOp(
             pcaprop.Variable('Z'),
             pcaprop.DisjunctionOp(
                 pcaprop.NegationOp(pcaprop.Variable('A')),
                 pcaprop.NegationOp(pcaprop.Variable('B')))))
Exemplo n.º 9
0
 def test_idempotence(self):
     self.assertTrue(
         pcabuilder.InitProp('A and A').idempotence() == pcaprop.Variable(
             'A'))
     self.assertTrue(
         pcabuilder.InitProp('not not (A and A)').idempotence() ==
         pcaprop.NegationOp(pcaprop.NegationOp(pcaprop.Variable('A'))))
     self.assertTrue(
         pcabuilder.InitProp('B or not (A and A)').idempotence() ==
         pcaprop.DisjunctionOp(pcaprop.Variable('B'),
                               pcaprop.NegationOp(pcaprop.Variable('A'))))
     self.assertTrue(
         pcabuilder.InitProp('B or not (A and ( A or A))').idempotence() ==
         pcaprop.DisjunctionOp(pcaprop.Variable('B'),
                               pcaprop.NegationOp(pcaprop.Variable('A'))))
     self.assertFalse(
         pcabuilder.InitProp('not not (A and B)').idempotence() ==
         pcaprop.NegationOp(pcaprop.NegationOp(pcaprop.Variable('A'))))
Exemplo n.º 10
0
async def exercise_eval(q_prop, methods, t_prop):
    try:
        q_proposition = _apply_methods(q_prop, methods)
        t_proposition = pcabuilder.InitProp(t_prop)
        return {
            'Result_Prop': str(q_proposition),
            'Result': q_proposition == t_proposition,
        }
    except Exception as e:
        raise PropException(error=str(e))
Exemplo n.º 11
0
async def calc_prop(prop):
    try:
        r = pcabuilder.InitProp(prop)
        return {
            'Proposition': str(r),
            'Satisfiable': bool(r.satisfiable()),
            'Tautology': bool(r.tautology()),
            'Contradiction': bool(r.contradiction()),
            'Variables': [variable.name for variable in r.unique_vars()],
            'Interpretations': [[bool(bool_val) for bool_val in interp] for interp in r.interpretations()]
        }
    except Exception as e:
        raise PropException(error=str(e))
Exemplo n.º 12
0
 def test_conjunction(self):
     to_test = [
         [pcaprop.FalseProp(),
          pcaprop.FalseProp(),
          pcaprop.FalseProp()],
         [pcaprop.FalseProp(),
          pcaprop.TrueProp(),
          pcaprop.FalseProp()],
         [pcaprop.TrueProp(),
          pcaprop.FalseProp(),
          pcaprop.FalseProp()],
         [pcaprop.TrueProp(),
          pcaprop.TrueProp(),
          pcaprop.TrueProp()]
     ]
     self.assertEqual(
         list(pcabuilder.InitProp('A and B').interpretations()), to_test)
     to_test = [[pcaprop.FalseProp(),
                 pcaprop.FalseProp()],
                [pcaprop.TrueProp(), pcaprop.TrueProp()]]
     self.assertEqual(
         list(pcabuilder.InitProp('A and true').interpretations()), to_test)
Exemplo n.º 13
0
 def test_taut(self):
     self.assertFalse(pcabuilder.InitProp('A').tautology())
     self.assertFalse(pcabuilder.InitProp('A or B').tautology())
     # Excluded middle
     self.assertTrue(pcabuilder.InitProp('A or not A').tautology())
     self.assertFalse(pcabuilder.InitProp('A and B implies C').tautology())
     # Contraposition
     self.assertTrue(
         pcabuilder.InitProp(
             '(A implies B) iff (not B implies not A)').tautology())
     # proof by exh
     self.assertTrue(
         pcabuilder.InitProp(
             '(((A or B) and (A implies C)) and (B implies C)) implies C').
         tautology())
Exemplo n.º 14
0
 def test_maximum(self):
     self.assertTrue(
         pcabuilder.InitProp('A or true').maximum() == pcaprop.TrueProp())
     self.assertTrue(
         pcabuilder.InitProp('true or (A iff B)').maximum() ==
         pcaprop.TrueProp())
     self.assertTrue(
         pcabuilder.InitProp('B iff (A and (true or (A iff B)))').maximum()
         == pcaprop.EquivalenceOp(pcaprop.Variable('B'),
                                  pcaprop.Variable('A')))
     self.assertTrue(
         pcabuilder.InitProp('A and true').maximum() == pcaprop.Variable(
             'A'))
     self.assertTrue(
         pcabuilder.InitProp('true and (A iff B)').maximum() == pcaprop.
         EquivalenceOp(pcaprop.Variable('A'), pcaprop.Variable('B')))
     self.assertTrue(
         pcabuilder.InitProp('A and (B and top)').maximum() == pcaprop.
         ConjunctionOp(pcaprop.Variable('A'), pcaprop.Variable('B')))
     self.assertTrue(
         pcabuilder.InitProp('Z iff (A or (B or top))').maximum() ==
         pcaprop.EquivalenceOp(pcaprop.Variable('Z'), pcaprop.TrueProp()))
Exemplo n.º 15
0
 def test_contr(self):
     self.assertFalse(pcabuilder.InitProp('A').contradiction())
     self.assertTrue(pcabuilder.InitProp('A and not A').contradiction())
     self.assertFalse(
         pcabuilder.InitProp(
             '(not(A and B)) ⇔ ((not A) or (not B))').contradiction())
Exemplo n.º 16
0
 def test_dijkstra_rule(self):
     self.assertTrue(
         pcabuilder.InitProp('(((A ∧ B) ⇔ A) ⇔ B) ⇔ (A ∨ B)').tautology())
Exemplo n.º 17
0
 def test_structural_equality(self):
     self.assertEqual(pcabuilder.InitProp('A or B'),
                      pcabuilder.InitProp('(A or B)'))
     self.assertNotEqual(pcabuilder.InitProp('A or B'),
                         pcabuilder.InitProp('B or A'))
Exemplo n.º 18
0
 def test_comb(self):
     to_test = [[
         pcaprop.FalseProp(),
         pcaprop.FalseProp(),
         pcaprop.FalseProp(),
         pcaprop.FalseProp(),
         pcaprop.TrueProp()
     ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ]]
     self.assertEqual(
         list(
             pcabuilder.InitProp(
                 'A and D or not C iff A implies B').interpretations()),
         to_test)
Exemplo n.º 19
0
 def test_contrapositive(self):
     self.assertEqual(
         list(pcabuilder.InitProp('C ⇒ (A ∧ B)').interpretations()),
         list(pcabuilder.InitProp('(C ⇒ A) ∧ (C ⇒ B)').interpretations()))
Exemplo n.º 20
0
 def test_sat(self):
     self.assertTrue(pcabuilder.InitProp('A').satisfiable())
     self.assertFalse(pcabuilder.InitProp('A and not A').satisfiable())
Exemplo n.º 21
0
 def test_distributivity(self):
     self.assertEqual(
         list(pcabuilder.InitProp('A ∨ (B ∧ C)').interpretations()),
         list(pcabuilder.InitProp('(A ∨ B) ∧ (A ∨ C)').interpretations()))