Пример #1
0
    def test_connective_to_onf(self):

        alpha = Symbol.Predicate('A', ['x'])
        beta = Symbol.Predicate('B', ['y'])
        charlie = Symbol.Predicate('C', ['u'])
        delta = Symbol.Predicate('D', ['z'])

        # Trivial case -- already in CNF
        two = (beta | alpha) & (delta)
        self.assertEqual(repr(two.to_onf()), '((B(y) | A(x)) & D(z))')

        two = (beta & alpha) | (delta)
        self.assertEqual(repr(two.to_onf()), '((D(z) | B(y)) & (D(z) | A(x)))')

        # Reversed case
        two = (delta) | (beta & alpha)
        self.assertEqual(repr(two.to_onf()), '((D(z) | B(y)) & (D(z) | A(x)))')

        # Nested distribution
        one = (alpha & beta) | (charlie & delta)
        self.assertEqual(
            repr(one.to_onf()),
            '((A(x) | C(u)) & (A(x) | D(z)) & (B(y) | C(u)) & (B(y) | D(z)))')

        # Nested distribution
        one = (alpha | (beta & (charlie | (delta & alpha))))
        self.assertEqual(
            repr(one.to_onf()),
            '((A(x) | B(y)) & (C(u) | A(x) | D(z)) & (C(u) | A(x) | A(x)))')
Пример #2
0
    def test_axiom_to_pcnf(self):
        a = Symbol.Predicate('A', ['x'])
        b = Symbol.Predicate('B', ['y'])
        c = Symbol.Predicate('C', ['z'])

        # Simple test of disjunction over conjunction
        axi_one = Axiom.Axiom(Quantifier.Universal(['x','y','z'], a | b & c))
        axi_one = axi_one.to_pcnf()
        self.assertEqual('∀(z,y,x)[((A(z) | B(y)) & (A(z) | C(x)))]', repr(axi_one))

        # Test recursive distribution 

        #axi_one = Axiom.Axiom(Quantifier.Universal(['x','y','z'], a | (b & (a | (c & b)))))
        #print(repr(axi_one))
        #self.assertEqual('', repr(axi_one.to_pcnf()))

        # Simple sanity check, it's already FF-PCNF
        axi_two = Axiom.Axiom(Quantifier.Universal(['x','y','z'], (a | b) & c))
        axi_two = axi_two.to_pcnf()
        self.assertEqual('∀(z,y,x)[(C(x) & (A(z) | B(y)))]', repr(axi_two))

        # Sanity check we remove functions
        c = Symbol.Predicate('C', ['z', Symbol.Function('F', ['z'])])
        axi_three = Axiom.Axiom(Quantifier.Universal(['x','y','z'], a | b & c))
        axi_three = axi_three.to_pcnf()
        self.assertEqual('∀(z,y,x,w,v)[((A(z) | ~C(w,v) | F(w,v)) & (A(z) | B(y)))]', repr(axi_three))
Пример #3
0
    def test_distribution(self):
        '''
        Ensure that distribution over conjunctions work
        '''
        #(b & (a | (c & b)))

        alpha = Symbol.Predicate('A', ['x'])
        beta = Symbol.Predicate('B', ['y'])
        charlie = Symbol.Predicate('C', ['t'])
        delta = Symbol.Predicate('D', ['z'])

        s = delta | (alpha & beta)
        ret = s.distribute(s.terms[0], s.terms[1])
        self.assertEqual(repr(ret), '((D(z) | A(x)) & (D(z) | B(y)))')

        s = (alpha | beta) & delta
        ret = s.distribute(s.terms[0], s.terms[1])
        self.assertEqual(repr(ret), '((D(z) & A(x)) | (D(z) & B(y)))')

        s = (alpha | beta) & (beta | delta)
        ret = s.distribute(s.terms[0], s.terms[1])
        self.assertEqual(repr(ret),
                         '(((A(x) | B(y)) & B(y)) | ((A(x) | B(y)) & D(z)))')

        # Simple case - single distribute
        s = beta | (alpha & (delta | charlie))
        ret = s.distribute(s.terms[0], s.terms[1])
        self.assertEqual('((B(y) | A(x)) & (B(y) | D(z) | C(t)))', repr(ret))

        # Slightly more complex
        s = (beta & charlie) | (alpha & (delta | charlie))
        ret = s.distribute(s.terms[0], s.terms[1])
        self.assertEqual(
            '(((B(y) & C(t)) | A(x)) & ((B(y) & C(t)) | D(z) | C(t)))',
            repr(ret))
Пример #4
0
    def test_predicate_form(self):
        '''
        Ensure that predicates take the correct repr form
        '''

        alpha = Symbol.Predicate('A', ['x'])
        self.assertEqual(repr(alpha), 'A(x)')

        beta = Symbol.Predicate('B', ['x', 'y'])
        self.assertEqual(repr(beta), 'B(x,y)')
Пример #5
0
    def test_axiom_function_replacement(self):
        f = Symbol.Function('f', ['x'])
        t = Symbol.Function('t', ['y'])
        a = Symbol.Predicate('A', [f])
        b = Symbol.Predicate('B', [f, t])

        axi = Axiom.Axiom(Quantifier.Universal(['x'], a | a & a))
        self.assertEqual(repr(axi), '∀(x)[(A(f(x)) | (A(f(x)) & A(f(x))))]')

        axi = Axiom.Axiom(Quantifier.Universal(['x', 'y'], b))
Пример #6
0
    def test_conjunction_form(self):
        """
        Ensure basic & operator overloading is working
        """

        alpha = Symbol.Predicate('A', ['x'])
        beta = Symbol.Predicate('B', ['x', 'y'])
        delta = Symbol.Predicate('D', ['z'])

        self.assertEqual(repr(alpha & beta), '(A(x) & B(x,y))')
        self.assertEqual(repr(alpha & beta & delta), '(A(x) & B(x,y) & D(z))')
Пример #7
0
    def test_disjunction_form(self):
        '''
        Ensure basic | operator overloading is working
        '''

        alpha = Symbol.Predicate('A', ['x'])
        beta = Symbol.Predicate('B', ['x', 'y'])
        delta = Symbol.Predicate('D', ['z'])

        self.assertEqual(repr(alpha | beta), '(A(x) | B(x,y))')
        self.assertEqual(repr(alpha | beta | delta), '(A(x) | B(x,y) | D(z))')
Пример #8
0
    def test_axiom_variable_standardize(self):

        a = Symbol.Predicate('A', ['x'])
        b = Symbol.Predicate('B', ['y', 'x'])
        c = Symbol.Predicate('C', ['a','b','c','d','e','f','g','h','i'])

        axi = Axiom.Axiom(Quantifier.Universal(['x'], a | a & a))
        self.assertEqual(repr(axi.standardize_variables()), '∀(z)[(A(z) | (A(z) & A(z)))]')

        axi = Axiom.Axiom(Quantifier.Universal(['x', 'y'], b))
        self.assertEqual(repr(axi.standardize_variables()), '∀(z,y)[B(y,z)]')

        axi = Axiom.Axiom(Quantifier.Existential(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], c))
        self.assertEqual(repr(axi.standardize_variables()), '∃(z,y,x,w,v,u,t,s,r)[C(z,y,x,w,v,u,t,s,r)]')
Пример #9
0
    def test_cnf_quantifier_simplfy(self):

        alpha = Symbol.Predicate('A', ['x'])
        beta = Symbol.Predicate('B', ['y'])

        uni_one = Quantifier.Universal(['x'], alpha)
        mixer = uni_one | beta
        uni_two = Quantifier.Universal(['y'], mixer)

        uni_nested = Quantifier.Universal(['z'],
                                          alpha & (beta | (alpha & uni_one)))
        self.assertEqual('∀(z)[(A(x) & (B(y) | (A(x) & ∀(x)[A(x)])))]',
                         repr(uni_nested))
        self.assertEqual('∀(z,x)[(A(x) & (B(y) | (A(x) & A(x))))]',
                         repr(uni_nested.simplify()))

        self.assertEqual(repr(uni_two), "∀(y)[(∀(x)[A(x)] | B(y))]")
        self.assertEqual(repr(uni_two.simplify()), "∀(y,x)[(B(y) | A(x))]")
Пример #10
0
    def test_mixed_form(self):
        '''
        Ensure that the &  and | operators work when chained
        '''

        alpha = Symbol.Predicate('A', ['x'])
        beta = Symbol.Predicate('B', ['x', 'y'])
        delta = Symbol.Predicate('D', ['z'])

        self.assertEqual(repr((alpha & beta) | delta),
                         '((A(x) & B(x,y)) | D(z))')
        self.assertEqual(repr(alpha & (beta | delta)),
                         '(A(x) & (B(x,y) | D(z)))')

        self.assertEqual(repr((alpha & beta) | (alpha & delta)),
                         '((A(x) & B(x,y)) | (A(x) & D(z)))')
        self.assertEqual(repr((alpha | beta) & (alpha | delta)),
                         '((A(x) | B(x,y)) & (A(x) | D(z)))')
Пример #11
0
    def test_axiom_simple_function_replacement(self):
        f = Symbol.Function('f', ['x'])
        t = Symbol.Function('t', ['y'])
        p = Symbol.Function('p', ['z'])
        a = Symbol.Predicate('A', [f, t, p])
        b = Symbol.Predicate('B', [f, t])
        c = Symbol.Predicate('C', [f])


        axi = Axiom.Axiom(Quantifier.Universal(['x', 'y', 'z'], a ))
        self.assertEqual(repr(axi.substitute_functions()), '∀(x,y,z)[∀(f2,t3,p4)[(~A(f2,t3,p4) | (f(x,f2) & t(y,t3) & p(z,p4)))]]')

        axi = Axiom.Axiom(Quantifier.Universal(['x',], ~c ))
        self.assertEqual(repr(axi.substitute_functions()), '∀(x)[~~∀(f5)[(C(f5) | f(x,f5))]]')

        c = Symbol.Predicate('C', [Symbol.Function('f', [Symbol.Function('g', [Symbol.Function('h', ['x'])])])])
        axi = Axiom.Axiom(Quantifier.Universal(['x'], c))
        self.assertEqual(repr(axi.substitute_functions()), '∀(x)[∀(f5,g6,h7)[(~C(f5) | (h(x,h7) & g(h7,g6) & f(g6,f5)))]]')
Пример #12
0
    def test_axiom_quantifier_coalesence(self):

        a = Symbol.Predicate('A', ['x'])
        b = Symbol.Predicate('B', ['y'])

        universal = Quantifier.Universal(['x'], a)
        universal_two = Quantifier.Universal(['y'], b)
        existential = Quantifier.Existential(['y'], b)
        existential_two = Quantifier.Existential(['x'], a)

        # Coalescence over conjunction should merge Universals
        conjunction = universal & universal_two & existential & existential_two
        self.assertEqual(repr(conjunction.coalesce()),
                         '(∃(y)[B(y)] & ∃(x)[A(x)] & ∀(x)[(B(x) & A(x))])')

        # Coalescence over disjunction should merge Existentials
        disjunction = universal | universal_two | existential | existential_two
        self.assertEqual(repr(disjunction.coalesce()),
                         '(∀(x)[A(x)] | ∀(y)[B(y)] | ∃(y)[(A(y) | B(y))])')
Пример #13
0
    def test_cnf_quantifier_scoping(self):

        a = Symbol.Predicate('A', ['x'])
        b = Symbol.Predicate('B', ['y'])
        c = Symbol.Predicate('C', ['z'])

        e = Quantifier.Existential(['x'], a)
        u = Quantifier.Universal(['y'], b)

        # Test the effect over an OR
        self.assertEqual('∃(x)[(A(x) | B(y))]', repr((e | b).rescope()))
        self.assertEqual('∀(y)[(B(y) | A(x))]', repr((u | a).rescope()))

        # Test the effect over an AND
        self.assertEqual('∃(x)[(A(x) & B(y))]', repr((e & b).rescope()))
        self.assertEqual('∀(y)[(B(y) & A(x))]', repr((u & a).rescope()))

        # Test with more than two to make sure things aren't dropped
        self.assertEqual('∀(y)[(B(y) & (A(x) | C(z) | B(y)))]',
                         repr((u & (a | c | b)).rescope()))
Пример #14
0
    def test_onf_detection(self):

        alpha = Symbol.Predicate('A', ['x'])
        beta = Symbol.Predicate('B', ['y'])
        delta = Symbol.Predicate('D', ['z'])

        uni = Quantifier.Universal(['x', 'y', 'z'], alpha | beta | delta)
        exi = Quantifier.Existential(['x','y','z'], alpha & beta | delta)

        self.assertEqual(alpha.is_onf(), True)
        self.assertEqual((alpha | beta).is_onf(), True)
        self.assertEqual((alpha & beta).is_onf(), True)
        self.assertEqual((alpha | (beta & delta)).is_onf(), False)
        self.assertEqual((alpha & (beta | delta)).is_onf(), True)
        self.assertEqual((~(alpha | beta)).is_onf(), False)
        self.assertEqual((~(alpha & beta)).is_onf(), False)

        self.assertEqual(uni.is_onf(), True)
        self.assertEqual(exi.is_onf(), False)

        # Note that is_onf() is not a recursive call, it's a top level feature
        # If will actually if you need an ONF axiom then create a Logical.Axiom and to_onf()
        self.assertEqual((alpha & (alpha | (beta & delta)) & delta).is_onf(), False)
Пример #15
0
def p_predicate(p):
    """
    predicate : LPAREN NONLOGICAL parameter RPAREN
    """

    p[0] = Symbol.Predicate(p[2], p[3])