def test_cnf_negation(self): ''' Ensure we can get into conjunctive normal form ''' alpha = Symbol.Predicate('A', ['x']) beta = Symbol.Predicate('B', ['y']) delta = Symbol.Predicate('D', ['z']) s = ~(Quantifier.Universal(['x', 'y', 'z'], (~(alpha | beta) & delta))) self.assertEqual(repr(s.push_complete()), "∃(x,y,z)[(A(x) | B(y) | ~D(z))]") s = ~(Quantifier.Universal(['x', 'y', 'z'], ~((alpha | beta) & delta))) self.assertEqual(repr(s.push_complete()), "∃(x,y,z)[((A(x) | B(y)) & D(z))]") s = ~((~alpha | ~beta) & ~delta) self.assertEqual(repr(s.push_complete()), "((A(x) & B(y)) | D(z))") ## Test to make sure the recursino into nested stuff actually work s = (~~~~~~~~~alpha).push_complete() self.assertEqual(repr(s), '~A(x)') s = (~~~~~~~~alpha).push_complete() self.assertEqual(repr(s), 'A(x)')
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))
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))
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_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_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))]")
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)))]]')
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_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 substitute_function(self, negated = False): ''' Find a function that's nested and replace it by adding a new variable and term ''' # TODO This a dirty hack because cyclic imports are painful import logical.Quantifier as Quantifier import logical.Negation as Negation import logical.Connective as Connective global gen def sub_functions(term, predicates, variables): ''' Does three things: (1) returns a variable that serves as the placeholder for the function. (2) adds a minted predicate to the accumulator (3) adds any the newly introduced variable for each function to the accumulator ''' # Singleton used to access an increasing integer sequence global gen # Base Case 0: I'm not event a function, I'm a variable if isinstance(term, str): # I'm a variable so I go in the variable accumulator variables.append(term) return term # Base Case 1: I'm a function with no nested functions! if isinstance(term, Function) and term.has_functions() == False: # Mint a new variable special new_variable = term.name.lower()[0] + gen() variables.append(new_variable) # Mint a new predicate with our original variables + the new one predicate_variables = term.variables predicate_variables.append(new_variable) predicate = Predicate(term.name, predicate_variables) predicates.append(predicate) # Return our fresh variable return new_variable # Recursive Case: I'm a function with nested functions! if isinstance(term, Function): #and term.has_functions() == True: # Still mint a new variable cuz I'm still a function new_variable = term.name.lower()[0] + gen() variables.append(new_variable) # Assemble my variables by a DFS down on my function / atom children predicate_variables = [sub_functions(x, predicates, variables) for x in term.variables] predicate_variables.append(new_variable) predicate = Predicate(term.name, predicate_variables) predicates.append(predicate) return new_variable predicate_accumulator = [] variable_accumulator = [] variables = [sub_functions(x, predicate_accumulator, variable_accumulator) for x in self.variables] if len(predicate_accumulator) > 1: term = Connective.Conjunction(predicate_accumulator) else: term = predicate_accumulator.pop() predicate = Predicate(self.name, variables) if negated: # The negation cancels out the normal conditional breakdown universal = Quantifier.Universal(variable_accumulator, predicate | term) else: universal = Quantifier.Universal(variable_accumulator, ~predicate | term) return universal, predicate_accumulator
def p_universal(p): """ universal : LPAREN FORALL LPAREN nonlogicals RPAREN axiom RPAREN """ p[0] = Quantifier.Universal(p[4], p[6])