def test_is_formula_exists(self): fol = self.fol john = ConstantTerm.fromString("john") not_a_term = ConstantTerm.fromString("NotATerm") x = Variable.fromString("x") y = Variable.fromString("y") right_equal = Equal(x, john) wrong_equal = Equal(x, not_a_term) self.assertTrue(fol.is_formula(Exists(x, right_equal))) self.assertFalse(fol.is_formula(Exists(x, wrong_equal))) self.assertTrue( fol.is_formula( Exists(x, PredicateFormula(PredicateSymbol("Person", 2), john, x)))) self.assertFalse( fol.is_formula( Exists( x, PredicateFormula(PredicateSymbol("Person", 3), john, x, john)))) self.assertFalse( fol.is_formula( Exists( x, PredicateFormula(PredicateSymbol("Person_fake", 2), john, x))))
def test_is_formula_and(self): fol = self.fol john = ConstantTerm.fromString("john") paul = ConstantTerm.fromString("paul") not_a_term = ConstantTerm.fromString("NotATerm") right_equal = Equal(john, paul) wrong_equal = Equal(john, not_a_term) self.assertTrue(fol.is_formula(And(right_equal, right_equal))) self.assertFalse(fol.is_formula(And(right_equal, Not(wrong_equal)))) self.assertTrue( fol.is_formula( And(right_equal, PredicateFormula(PredicateSymbol("Person", 2), john, paul)))) self.assertFalse( fol.is_formula( And( right_equal, PredicateFormula(PredicateSymbol("Person", 3), john, paul, john)))) self.assertFalse( fol.is_formula( And( right_equal, PredicateFormula(PredicateSymbol("Person_fake", 2), john, paul))))
def test_is_formula_predicate(self): # TODO: Cover the FunctionTerm case (not only ConstantTerm) fol = self.fol Person_pred_sym = self.Person_pred_sym john = ConstantTerm.fromString("john") paul = ConstantTerm.fromString("paul") not_a_term = ConstantTerm.fromString("NotATerm") # Notice: only syntactic check. # test terms self.assertTrue( fol.is_formula(PredicateFormula(Person_pred_sym, john, paul))) self.assertFalse( fol.is_formula(PredicateFormula(Person_pred_sym, not_a_term, paul))) # test predicate symbol self.assertTrue( self.fol.is_formula( PredicateFormula(PredicateSymbol("Person", 2), john, paul))) self.assertFalse( self.fol.is_formula( PredicateFormula(PredicateSymbol("Person", 3), john, paul, john))) self.assertFalse( self.fol.is_formula( PredicateFormula(PredicateSymbol("Person_fake", 2), john, paul)))
def test_is_formula_derived(self): fol = self.fol john = ConstantTerm.fromString("john") not_a_term = ConstantTerm.fromString("NotATerm") x = Variable.fromString("x") y = Variable.fromString("y") right_equal = Equal(x, john) wrong_equal = Equal(x, not_a_term) self.assertTrue(fol.is_formula(TrueFormula())) self.assertTrue(fol.is_formula(FalseFormula())) self.assertTrue(fol.is_formula(Or(right_equal, right_equal))) self.assertFalse(fol.is_formula(Or(right_equal, wrong_equal))) self.assertTrue(fol.is_formula(Or(right_equal, right_equal))) self.assertFalse(fol.is_formula(Or(right_equal, wrong_equal))) self.assertTrue(fol.is_formula(Implies(right_equal, right_equal))) self.assertFalse(fol.is_formula(Implies(right_equal, wrong_equal))) self.assertTrue(fol.is_formula(Equivalence(right_equal, right_equal))) self.assertFalse(fol.is_formula(Equivalence(right_equal, wrong_equal))) self.assertTrue( fol.is_formula( ForAll(x, PredicateFormula(PredicateSymbol("Person", 2), john, x)))) self.assertFalse( fol.is_formula( ForAll( x, PredicateFormula(PredicateSymbol("Person", 3), john, x, john)))) self.assertFalse( fol.is_formula( ForAll( x, PredicateFormula(PredicateSymbol("Person_fake", 2), john, x))))
def test_truth(self): w = Variable.fromString("w") Person_x_20 = PredicateFormula(self.Person_pred_sym, self.x, ConstantTerm.fromString("20")) Person_x_y = PredicateFormula(self.Person_pred_sym, self.x, self.y) not_Person_x_21 = Not( PredicateFormula(self.Person_pred_sym, self.x, ConstantTerm.fromString("21"))) y_equal_20 = Equal(self.y, ConstantTerm.fromString("20")) x_equal_john = Equal(self.x, ConstantTerm.fromString("john")) x_equal_x = Equal(self.x, self.x) x_equal_y = Equal(self.x, self.y) x_equal_z = Equal(self.x, self.z) x_lives_w = PredicateFormula(self.Lives_pred_sym, self.x, w) x_lives_y = PredicateFormula(self.Lives_pred_sym, self.x, self.y) x_lives_ny = PredicateFormula(self.Lives_pred_sym, self.x, ConstantTerm.fromString("ny")) x_lives_paris = PredicateFormula(self.Lives_pred_sym, self.x, ConstantTerm.fromString("paris")) w_lives_z = PredicateFormula(self.Lives_pred_sym, w, self.z) exists_w__x_lives_w = Exists(w, x_lives_w) exists_y__x_lives_y = Exists(self.y, x_lives_y) exists_z_exists_w__w_lives_z = Exists(self.z, Exists(w, w_lives_z)) exists_x__x_equal_x = Exists(self.x, x_equal_x) exists_x__x_equal_y = Exists(self.x, x_equal_y) exists_x__x_equal_john_and_Lives_x_paris = Exists( self.x, And(x_equal_john, x_lives_paris)) exists_x__x_equal_john_and_exists_x__Lives_x_ny = And( Exists(self.x, x_equal_john), Exists(self.x, x_lives_ny)) exists_x__x_equal_x_and_exists_x__Lives_x_ny = And( exists_x__x_equal_x, Exists(self.x, x_lives_ny)) forall_x__x_equal_x = ForAll(self.x, x_equal_x) forall_x__x_equal_y = ForAll(self.x, x_equal_y) not_forall_x__x_equal_x = Not(forall_x__x_equal_x) # Person(x, 20) # x = "john" self.assertTrue(self.FOL.truth(Person_x_20, self.assignment)) # ~Person(x, 21) # x = "john" self.assertTrue(self.FOL.truth(not_Person_x_21, self.assignment)) # Equals self.assertTrue(self.FOL.truth(y_equal_20, self.assignment)) self.assertTrue(self.FOL.truth(x_equal_x, self.assignment)) self.assertFalse(self.FOL.truth(x_equal_y, self.assignment)) self.assertFalse(self.FOL.truth(x_equal_z, self.assignment)) # y == 20 and x == "john" and Person(x, y) self.assertTrue( self.FOL.truth(And(y_equal_20, And(x_equal_john, Person_x_y)), self.assignment)) self.assertTrue( self.FOL.truth(Or(Person_x_20, x_equal_john), self.assignment)) # De Morgan on previous formula # Not (Not y == 20 or Not x == "john" ot Not Person(x, y) self.assertTrue( self.FOL.truth( Not(Or(Not(y_equal_20), Or(Not(x_equal_john), Not(Person_x_y)))), self.assignment)) # Or with the last formula true self.assertTrue( self.FOL.truth( Or(Equal(self.x, self.y), Or(Equal(self.x, self.y), Equal(self.z, self.z))), self.assignment)) # (y==20 and x=="john") => Person(x,y) self.assertTrue( self.FOL.truth(Implies(And(y_equal_20, x_equal_john), Person_x_y), self.assignment)) self.assertTrue( self.FOL.truth(Implies(Not(x_equal_x), Person_x_y), self.assignment)) # Exists self.assertTrue(self.FOL.truth(exists_w__x_lives_w, self.assignment)) self.assertTrue(self.FOL.truth(exists_x__x_equal_x, self.assignment)) self.assertTrue(self.FOL.truth(exists_x__x_equal_y, self.assignment)) # quantified variable not present in the formula self.assertTrue(self.FOL.truth(exists_y__x_lives_y, self.assignment)) # 2 quantified variables self.assertTrue( self.FOL.truth(exists_z_exists_w__w_lives_z, self.assignment)) # annidate exists self.assertTrue( self.FOL.truth(exists_x__x_equal_john_and_exists_x__Lives_x_ny, self.assignment)) self.assertTrue( self.FOL.truth(exists_x__x_equal_x_and_exists_x__Lives_x_ny, self.assignment)) self.assertFalse( self.FOL.truth(exists_x__x_equal_john_and_Lives_x_paris, self.assignment)) # ForAll self.assertTrue(self.FOL.truth(forall_x__x_equal_x, self.assignment)) self.assertFalse(self.FOL.truth(forall_x__x_equal_y, self.assignment)) self.assertFalse( self.FOL.truth(not_forall_x__x_equal_x, self.assignment))
def setUp(self): """Set up symbols, terms and formulas. Both legal and illegal, according to some FOL system""" # Symbols self.a_sym = Symbol('a') self.b_sym = Symbol('b') self.const_sym = ConstantSymbol("Const") self.fun_sym = FunctionSymbol("Fun", 3) self.predicate_sym = PredicateSymbol("Predicate", 2) self.A = PredicateSymbol("A", 1) # Terms self.a = Variable(self.a_sym) self.b = Variable(self.b_sym) self.c = Variable.fromString("c") self.const = ConstantTerm(self.const_sym) self.fun_abc = FunctionTerm(self.fun_sym, self.a, self.b, self.c) # Formulas self.predicate_ab = PredicateFormula(self.predicate_sym, self.a, self.b) self.predicate_ac = PredicateFormula(self.predicate_sym, self.a, self.c) self.A_a = PredicateFormula(self.A, self.a) self.a_equal_a = Equal(self.a, self.a) self.b_equal_c = Equal(self.b, self.c) self.neg_a_equal_a = Not(self.a_equal_a) self.neg_Aa = Not(self.A_a) self.Aa_and_b_equal_c = And(self.A_a, self.b_equal_c) self.Aa_or_b_equal_c = Or(self.A_a, self.b_equal_c) self.Aa_implies_b_equal_c = Implies(self.A_a, self.b_equal_c) self.exists_a_predicate_ab = Exists(self.a, self.predicate_ab) self.forall_b_exists_a_predicate_ab = ForAll( self.b, self.exists_a_predicate_ab) # FOL self.vars = {self.a, self.b, self.c} self.functions = {self.const_sym, self.fun_sym} self.predicates = {self.predicate_sym, self.A} self.alphabet = FOLAlphabet(self.functions, self.predicates) self.myFOL = FOL(self.alphabet) # define dummy stuff # does not belong to myFOL. They are used for test membership to myFOL self.dummy_variable = Variable.fromString( "ThisVariableDoesNotBelongToFOLSystem") self.dummy_fun_sym = FunctionSymbol( "ThisFunctionDoesNotBelongToFOLSystem", 3) self.dummy_constant_sym = ConstantSymbol( "ThisConstDoesNotBelongToFOLSystem") self.dummy_predicate_sym = PredicateSymbol( "ThisPredicateDoesNotBelongToFOLSystem", 2) self.dummy_fun = FunctionTerm(self.dummy_fun_sym, self.a, self.b, self.dummy_variable) self.dummy_constant = ConstantTerm(self.dummy_constant_sym) self.dummy_predicate = PredicateFormula(self.dummy_predicate_sym, self.a, self.b) self.dummy_predicate_only_one_symbol_false = PredicateFormula( self.predicate_sym, self.dummy_variable, self.dummy_constant) self.dummy_equal = Equal(self.c, self.dummy_constant) self.dummy_neg = Not(self.dummy_predicate_only_one_symbol_false) self.dummy_and = And(self.dummy_predicate, self.predicate_ab) self.dummy_or = Or(self.dummy_predicate_only_one_symbol_false, self.predicate_ac) self.dummy_exists = Exists(self.dummy_variable, self.dummy_predicate_only_one_symbol_false) self.dummy_forall = ForAll(self.b, self.dummy_predicate)
def test_eq(self): # Symbols self.assertEqual(self.a_sym, self.a_sym) self.assertEqual(self.a_sym, Symbol("a")) self.assertEqual(self.const_sym, ConstantSymbol("Const")) self.assertEqual(self.fun_sym, FunctionSymbol("Fun", 3)) self.assertEqual(self.predicate_sym, PredicateSymbol("Predicate", 2)) self.assertNotEqual(self.a_sym, Symbol("c")) self.assertNotEqual(self.const_sym, ConstantSymbol("Another_Const")) self.assertNotEqual(self.fun_sym, FunctionSymbol("Another_Fun", 3)) self.assertNotEqual(self.fun_sym, FunctionSymbol("Fun", 2)) self.assertNotEqual(self.predicate_sym, PredicateSymbol("Another_Predicate", 2)) self.assertNotEqual(self.predicate_sym, PredicateSymbol("Predicate", 1)) self.assertNotEqual(self.a_sym, self.fun_sym) self.assertNotEqual(self.const_sym, self.fun_sym) # Terms self.assertEqual(self.a, self.a) self.assertEqual(self.a, Variable.fromString("a")) self.assertEqual(self.const, ConstantTerm(ConstantSymbol("Const"))) self.assertEqual( self.fun_abc, FunctionTerm(FunctionSymbol("Fun", 3), self.a, self.b, self.c)) self.assertNotEqual(self.a, self.b) self.assertNotEqual(self.a, Variable.fromString("c")) self.assertNotEqual(self.const, ConstantTerm(ConstantSymbol("Another_Const"))) self.assertNotEqual( self.fun_abc, FunctionTerm(FunctionSymbol("Another_Fun", 3), self.a, self.b, self.c)) self.assertNotEqual( self.fun_abc, FunctionTerm(FunctionSymbol("Fun", 3), self.a, self.b, self.a)) self.assertNotEqual(self.a, self.fun_abc) # Formulas self.assertEqual( self.predicate_ab, PredicateFormula(PredicateSymbol("Predicate", 2), Variable.fromString("a"), Variable.fromString("b"))) self.assertEqual( self.A_a, PredicateFormula(PredicateSymbol("A", 1), Variable.fromString("a"))) self.assertEqual( self.b_equal_c, Equal(Variable.fromString("b"), Variable.fromString("c"))) self.assertEqual( self.neg_a_equal_a, Not(Equal(Variable.fromString("a"), Variable.fromString("a")))) self.assertEqual( self.forall_b_exists_a_predicate_ab, ForAll( Variable.fromString("b"), Exists( Variable.fromString("a"), PredicateFormula(PredicateSymbol("Predicate", 2), Variable.fromString("a"), Variable.fromString("b"))))) self.assertNotEqual( self.predicate_ab, PredicateFormula(PredicateSymbol("Predicate", 2), Variable.fromString("a"), Variable.fromString("c"))) self.assertNotEqual( self.predicate_ab, PredicateFormula(PredicateSymbol("Another_Predicate", 2), Variable.fromString("a"), Variable.fromString("c"))) self.assertNotEqual( self.A_a, PredicateFormula(PredicateSymbol("A", 1), Variable.fromString("b"))) self.assertNotEqual( self.b_equal_c, Equal(Variable.fromString("b"), Variable.fromString("b"))) self.assertNotEqual( self.neg_a_equal_a, Not(Equal(Variable.fromString("b"), Variable.fromString("a")))) self.assertNotEqual( self.forall_b_exists_a_predicate_ab, ForAll( Variable.fromString("b"), Exists( Variable.fromString("a"), PredicateFormula(PredicateSymbol("ANOTHER_PREDICATE", 2), Variable.fromString("a"), Variable.fromString("b")))))