Ejemplo n.º 1
0
    def test_axiom_connecive_rescoping(self):

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

        universal = Universal(['x'], a)
        existential = Existential(['y'], b)

        conjunction = universal & existential
        disjunction = universal | existential

        # Ensure we handle single quantifier case
        self.assertEqual(repr((universal & b).rescope()),
                         '∀(x)[(A(x) & B(y))]')
        self.assertEqual(repr((existential & a).rescope()),
                         '∃(y)[(B(y) & A(x))]')
        self.assertEqual(repr((universal | b).rescope()),
                         '∀(x)[(A(x) | B(y))]')
        self.assertEqual(repr((existential | a).rescope()),
                         '∃(y)[(B(y) | A(x))]')

        # Ensure we catch error condition where lookahead is needed
        self.assertRaises(ValueError, (existential | universal).rescope)

        # Ensure that we can promote Universals when a conjunction lives above us
        top = a & disjunction
        self.assertEqual(repr(disjunction.rescope(top)),
                         '∀(x)[∃(y)[(A(x) | B(y))]]')

        # Ensure that we can promote Existentials when a conjunction lives above us
        top = a | conjunction
        self.assertEqual(repr(conjunction.rescope(top)),
                         '∃(y)[∀(x)[(B(y) & A(x))]]')
Ejemplo n.º 2
0
    def test_axiom_quantifier_coalesence(self):

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

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

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

        # Reduce over disjunction should coealesce Existentials and merge Universals
        disjunction = universal | universal_two | existential | existential_two
        self.assertEqual(repr(disjunction.coalesce()),
                         '(∃(y)[(A(y) | B(y))] | ∀(x,y)[(A(x) | B(y))])')
Ejemplo n.º 3
0
    def test_onf_detection(self):

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

        uni = Universal(['x', 'y', 'z'], alpha | beta | delta)
        exi = 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)
Ejemplo n.º 4
0
    def test_axiom_variable_standardize(self):

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

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

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

        axi = Axiom(
            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)]')
Ejemplo n.º 5
0
    def test_quantifiers(self):

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

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

        self.assertEqual(repr(uni), "∀(x,y,z)[(A(x) | B(y) | D(z))]")
        self.assertEqual(repr(exi), "∃(x,y,z)[(A(x) & B(y) & D(z))]")

        self.assertEqual(repr(~uni), "~∀(x,y,z)[(A(x) | B(y) | D(z))]")
        self.assertEqual(repr(~exi), "~∃(x,y,z)[(A(x) & B(y) & D(z))]")

        self.assertEqual(repr((~uni).push()),
                         "∃(x,y,z)[~(A(x) | B(y) | D(z))]")
        self.assertEqual(repr((~exi).push()),
                         "∀(x,y,z)[~(A(x) & B(y) & D(z))]")
Ejemplo n.º 6
0
    def test_cnf_quantifier_scoping(self):

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

        e = Existential(['x'], a)
        u = 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()))
Ejemplo n.º 7
0
def p_existential(p):
    """
    existential : LPAREN EXISTS LPAREN nonlogicals RPAREN axiom RPAREN
    """

    p[0] = Existential(p[4], p[6])