def test_axiom_connecive_rescoping(self): a = Symbol.Predicate('A', ['x']) b = Symbol.Predicate('B', ['y']) universal = Quantifier.Universal(['x'], a) existential = Quantifier.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))]]')
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))])')
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)]')
def test_quantifiers(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(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))]")
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()))
def push(self): ''' Push negation inwards and apply to all children ''' # Can be a conjunction or disjunction # Can be a single predicate # Can be a quantifier if isinstance(self.term(), Connective.Conjunction): ret = Connective.Disjunction([Negation(x) for x in self.term().get_term()]) elif isinstance(self.term(), Connective.Disjunction): ret = Connective.Conjunction([Negation(x) for x in self.term().get_term()]) elif isinstance(self.term(), Symbol.Predicate): ret = self elif isinstance(self.term(), Quantifier.Existential): ret = Quantifier.Universal(self.term().variables, Negation(self.term().get_term())) elif isinstance(self.term(), Quantifier.Universal): ret = Quantifier.Existential(self.term().variables, Negation(self.term().get_term())) elif isinstance(self.term(), Negation): ret = self.term().term() else: raise ValueError("Negation onto unknown type!", self.term) return copy.deepcopy(ret)
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)
def p_existential(p): """ existential : LPAREN EXISTS LPAREN nonlogicals RPAREN axiom RPAREN """ p[0] = Quantifier.Existential(p[4], p[6])