Example #1
0
 def test_final_3_7():
     t = Function("f", [Number("0")])
     t_ = Function("g", [Number("1")])
     try:
         interpreter.unify(t, t_)
         assert False
     except Not_unifiable:
         assert True
Example #2
0
 def test_final_3_8():
     u = {(Variable("X")): (Variable("Y"))}
     u_ = {(Variable("Y")): (Variable("X"))}
     t = Function("f", [Variable("X")])
     t_ = Function("f", [Variable("Y")])
     #print("8")
     assert (interpreter.unify(t, t_) == u
             or interpreter.unify(t, t_) == u_)
Example #3
0
 def test_variables_of_clause(self):
     c = Rule(
         Function("p",
                  [Variable("X"), Variable("Y"),
                   Atom("a")]),
         RuleBody([Function(
             "q", [Atom("a"), Atom("b"), Atom("a")])]))
     #assert
     (self.variables_of_clause(c) == set([Variable("X"), Variable("Y")]))
Example #4
0
 def test_final_2_3():
     s = {Variable(("Y")): Number("0"), Variable("X"): Variable(("Y"))}
     r = Rule((Function(
         "p", [Variable("X"), Variable("Y"),
               Atom("a")])), RuleBody([]))
     r_ = Rule(
         (Function("p", [Variable("Y"),
                         Number("0"), Atom("a")])), RuleBody([]))
     assert (interpreter.substitute_in_clause(s, r) == r_)
Example #5
0
 def test_final_2_4():
     s = {Variable(("Y")): Number("0"), Variable("X"): Variable(("Y"))}
     p = Function("p", [Variable("X"), Variable(("Y")), Atom(("a"))])
     q = Function("q", [Atom(("a")), Atom(("b")), Atom(("a"))])
     p_ = Function("p", [Variable(("Y")), Number("0"), Atom(("a"))])
     q_ = Function("q", [Atom(("a")), Atom(("b")), Atom(("a"))])
     r = Rule(p, RuleBody([q]))
     r_ = Rule(p_, RuleBody([q_]))
     assert (interpreter.substitute_in_clause(s, r) == r_)
Example #6
0
 def test_final_3_9():
     t1 = Function("f", [Variable("X"), Variable("Y"), Variable("Y")])
     t2 = Function("f", [Variable("Y"), Variable("Z"), Atom("a")])
     u = {
         Variable("X"): Atom("a"),
         Variable("Y"): Atom("a"),
         Variable("Z"): Atom("a")
     }
     #print("9")
     assert (interpreter.unify(t1, t2) == u)
Example #7
0
 def test_substitute_in_clause(self):
     s = {Variable("Y"): Number(0), Variable("X"): Variable("Y")}
     p = Function("p", [Variable("X"), Variable("Y"), Atom("a")])
     q = Function("q", [Atom("a"), Atom("b"), Atom("a")])
     p_ = Function("p", [Variable("Y"), Number(0), Atom("a")])
     q_ = Function("q", [Atom("a"), Atom("b"), Atom("a")])
     r = Rule(p, [q])
     r_ = Rule(p_, [q_])
     #assert
     (self.substitute_in_clause(s, r) == r_)
Example #8
0
 def test_unify(self):
     t1 = Function("f", [Variable("X"), Variable("Y"), Variable("Y")])
     t2 = Function("f", [Variable("Y"), Variable("Z"), Atom("a")])
     u = {
         Variable("X"): Atom("a"),
         Variable("Y"): Atom("a"),
         Variable("Z"): Atom("a")
     }
     #assert
     (self.unify(t1, t2) == u)
Example #9
0
 def test_final_4_1():
     print(
         "\n\n################################################################"
     )
     print(
         "###### Testing the non-deterministic abstract interpreter ######")
     print(
         "################################################################")
     print(f"Program: {list2str(psimple)}")
     g = [Function("f", [Atom("a"), Atom("b")])]
     print(f"Goal: {list2str(g)}")
     g_ = interpreter.nondet_query(psimple, g)
     assert (g_ == [Function("f", [Atom("a"), Atom("b")])])
     print(f"Solution: {list2str(g_)}")
Example #10
0
 def test_challenge_1():
     print("\n\n###################################################")
     print("###### Testing the deterministic interpreter ######")
     print("###################################################")
     print(f"Program: {list2str(psimple)}")
     # Tests query failure
     g = [Function("f", [Atom("a"), Atom("c")])]
     print(f"Goal: {list2str(g)}")
     assert (interpreter.det_query(psimple, g) == [])
     print("Solution: Empty solution")
Example #11
0
 def test_final_2_2():
     s = {Variable(("Y")): Number("0"), Variable("X"): Variable(("Y"))}
     t = Function("p", [Variable("X"), Variable("Y"), Atom("a")])
     t_ = Function("p", [Variable("Y"), Number("0"), Atom("a")])
     assert (interpreter.substitute_in_term(s, t) == t_)
Example #12
0
 def test_final_1_4():
     h = Function("p", [Variable("X"), Variable("Y"), Atom("a")])
     b = [Function("q", [Atom("a"), Atom("b"), Atom("a")])]
     r = Rule(h, RuleBody(b))
     assert (interpreter.variables_of_clause(r) == set(
         [Variable("X"), Variable("Y")]))
Example #13
0
 def append(x, y, z):
     return Function("append", [x, y, z])
Example #14
0
 def test_final_1_3():
     r = Rule((Function(
         "p", [Variable("X"), Variable("Y"),
               Atom("a")])), RuleBody([]))
     assert ((interpreter.variables_of_clause(r) == set(
         [Variable("X"), Variable("Y")])))
Example #15
0
 def father(x, y):
     return Function("father", [x, y])
Example #16
0
 def cons(h, t):
     return Function("cons", [h, t])
Example #17
0
 def ancestor(x, y):
     return Function("ancestor", [x, y])
Example #18
0
 def test_occurs_check(self):
     v = Variable("E")
     t = Function("cons", [Atom("a"), Atom("b"), Variable("E")])
     #assert
     (self.occurs_check(v, t))
Example #19
0
 def term(self, term1, relation, term2):
     return Function(relation, [term1, term2])
Example #20
0
 def test_variables_of_term(self):
     t = Function("f", [Variable("X"), Variable("Y"), Atom("a")])
     #assert
     (self.variables_of_term(t) == set([Variable("X"), Variable("Y")]))
Example #21
0
    def test_final_3_9():
        t1 = Function("f", [Variable("X"), Variable("Y"), Variable("Y")])
        t2 = Function("f", [Variable("Y"), Variable("Z"), Atom("a")])
        u = {
            Variable("X"): Atom("a"),
            Variable("Y"): Atom("a"),
            Variable("Z"): Atom("a")
        }
        #print("9")
        assert (interpreter.unify(t1, t2) == u)

    def list2str(l):
        return ('(' + (',' + ' ').join(list(map(str, l))) + ')')

    # Test on a simple program
    psimple = [Rule(Function("f", [Atom("a"), Atom("b")]), RuleBody([]))]

    def test_final_4_1():
        print(
            "\n\n################################################################"
        )
        print(
            "###### Testing the non-deterministic abstract interpreter ######")
        print(
            "################################################################")
        print(f"Program: {list2str(psimple)}")
        g = [Function("f", [Atom("a"), Atom("b")])]
        print(f"Goal: {list2str(g)}")
        g_ = interpreter.nondet_query(psimple, g)
        assert (g_ == [Function("f", [Atom("a"), Atom("b")])])
        print(f"Solution: {list2str(g_)}")
Example #22
0
 def test_final_4_3():
     g = [Function("f", [Variable("X"), Variable("Y")])]
     print(f"Goal: {list2str(g)}")
     g_ = interpreter.nondet_query(psimple, g)
     assert (g_ == [Function("f", [Atom("a"), Atom("b")])])
     print(f"Solution: {list2str(g_)}")
Example #23
0
 def test_final_1_2():
     assert ((interpreter.variables_of_term(
         Function("p",
                  [Variable("X"), Variable("Y"),
                   Atom("a")]))) == set([Variable("X"),
                                         Variable("Y")]))
Example #24
0
 def predicate(self, relation, terms=[]):
     return Function(relation, terms)
Example #25
0
 def test_substitute_in_term(self):
     s = {Variable("Y"): Number(0), Variable("X"): Variable("Y")}
     t = Function("f", [Variable("X"), Variable("Y"), Atom("a")])
     t_ = Function("f", [Variable("Y"), Number(0), Atom("a")])
     #assert
     (self.substitute_in_term(s, t) == t_)