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
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_)
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")]))
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_)
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_)
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 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_)
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)
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_)}")
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")
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_)
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")]))
def append(x, y, z): return Function("append", [x, y, z])
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")])))
def father(x, y): return Function("father", [x, y])
def cons(h, t): return Function("cons", [h, t])
def ancestor(x, y): return Function("ancestor", [x, y])
def test_occurs_check(self): v = Variable("E") t = Function("cons", [Atom("a"), Atom("b"), Variable("E")]) #assert (self.occurs_check(v, t))
def term(self, term1, relation, term2): return Function(relation, [term1, term2])
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")]))
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_)}")
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_)}")
def test_final_1_2(): assert ((interpreter.variables_of_term( Function("p", [Variable("X"), Variable("Y"), Atom("a")]))) == set([Variable("X"), Variable("Y")]))
def predicate(self, relation, terms=[]): return Function(relation, terms)
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_)