Пример #1
0
    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))))
Пример #2
0
    def setUp(self):
        self.Person_pred_sym = PredicateSymbol("Person", 2)
        self.Lives_pred_sym = PredicateSymbol("Lives", 2)
        self.LivesIn_fun_sym = FunctionSymbol("LivesIn", 1)

        self.objects = {"john", "paul", "george", "joseph"}
        self.person_tuples = {("john", 20), ("paul", 21), ("george", 22),
                              ("joseph", 23)}
        self.lives_tuples = {("john", "ny"), ("paul", "london"),
                             ("george", "paris"), ("joseph", "paris")}

        self.livesin_function_dictionary = {
            ("john", ): "ny",
            ("paul", ): "london",
            ("george", ): "paris",
            ("joseph", ): "paris"
        }

        self.Person = Relation(self.Person_pred_sym, self.person_tuples)
        self.Lives = Relation(self.Lives_pred_sym, self.lives_tuples)
        self.LivesIn = Function(self.LivesIn_fun_sym,
                                self.livesin_function_dictionary)

        self.I = Interpretation.fromRelationsAndFunctions(
            {self.LivesIn}, {self.Person, self.Lives})
        self.alphabet = self.I.alphabet
        self.fol = FOL(self.alphabet)
Пример #3
0
    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))))
Пример #4
0
    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)))
Пример #5
0
    def test_function_getter(self):
        the_same_tuples = {("john", 20), ("paul", 21), ("george", 22),
                           ("joseph", 23)}
        different_tuples = {("john", 20), ("paul", 21), ("george", 22),
                            ("joseph", 20)}

        the_same_function = Relation(PredicateSymbol("Person", 2),
                                     the_same_tuples)
        the_same_function_but_different_symbol = Relation(
            PredicateSymbol("Another_Person", 2), the_same_tuples)
        the_same_function_but_different_tuples = Relation(
            PredicateSymbol("Person", 2), different_tuples)

        self.assertEqual(self.I.getRelation(self.Person_pred_sym),
                         the_same_function)
        self.assertNotEqual(self.I.getRelation(self.Person_pred_sym),
                            the_same_function_but_different_symbol)
        self.assertNotEqual(self.I.getRelation(self.Person_pred_sym),
                            the_same_function_but_different_tuples)
Пример #6
0
    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))))
Пример #7
0
    def test_relation_eq(self):
        relation = Relation(PredicateSymbol("Predicate", 1), {("string_1", ),
                                                              ("string_2", ),
                                                              ("string_3", )})
        the_same_relation = Relation(PredicateSymbol("Predicate", 1),
                                     {("string_1", ), ("string_2", ),
                                      ("string_3", )})
        the_same_relation_but_different_symbol = Relation(
            PredicateSymbol("Another_Predicate", 1), {("string_1", ),
                                                      ("string_2", ),
                                                      ("string_3", )})
        the_same_relation_but_different_tuples = Relation(
            PredicateSymbol("Predicate", 1), {("string_1", ), ("string_2", ),
                                              ("different_string_3", )})

        self.assertEqual(relation, the_same_relation)
        self.assertNotEqual(relation, "NotARelation")
        self.assertNotEqual(relation, the_same_relation_but_different_symbol)
        self.assertNotEqual(relation, the_same_relation_but_different_tuples)
        # Test hash
        self.assertEqual(len({self.LivesIn}), len({self.LivesIn,
                                                   self.LivesIn}))
Пример #8
0
    def setUp(self):
        """Set up test fixtures, if any."""

        self.Person_pred_sym = PredicateSymbol("Person", 2)
        self.Lives_pred_sym = PredicateSymbol("Lives", 2)
        self.LivesIn_fun_sym = FunctionSymbol("LivesIn", 1)

        self.objects = {"john", "paul", "george", "joseph"}
        self.person_tuples = {("john", 20), ("paul", 21), ("george", 22),
                              ("joseph", 23)}
        self.lives_tuples = {("john", "ny"), ("paul", "london"),
                             ("george", "paris"), ("joseph", "paris")}

        self.livesin_function_dictionary = {
            ("john", ): "ny",
            ("paul", ): "london",
            ("george", ): "paris",
            ("joseph", ): "paris"
        }

        self.Person = Relation(self.Person_pred_sym, self.person_tuples)
        self.Lives = Relation(self.Lives_pred_sym, self.lives_tuples)
        self.LivesIn = Function(self.LivesIn_fun_sym,
                                self.livesin_function_dictionary)

        self.I = Interpretation.fromRelationsAndFunctions(
            {self.LivesIn}, {self.Person, self.Lives})
        self.alphabet = self.I.alphabet
        self.FOL = FOL(self.alphabet)

        self.x = Variable.fromString("x")
        self.y = Variable.fromString("y")
        self.z = Variable.fromString("z")
        self.var2obj = {self.x: "john", self.y: 20, self.z: "ny"}

        self.assignment = Assignment(self.var2obj, self.I)
Пример #9
0
    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)
Пример #10
0
    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")))))
Пример #11
0
 def fromString(cls, name: str, *args: Term):
     return PredicateFormula(PredicateSymbol(name, len(args)), *args)