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_challenge_4(): print(f"\nProgram: {list2str(pappend)}") # Tests choice points g = [ append(Variable("X"), (Variable("Y")), (cons(Number("1"), (cons(Number("2"), (cons(Number("3"), nil))))))) ] print(f"Goal: {list2str(g)}") g_ = interpreter.det_query(pappend, g) assert (len(g_) == 4) for sg in g_: print(f"Solution: {list2str(sg)}")
def test_final_4_6(): g = [ancestor(Variable("X"), Atom("robb"))] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(pstark, g) print(f"Solution: {list2str(g_)}") assert (g_ == [ancestor(Atom("ned"), Atom("robb"))] or g_ == [ancestor(Atom("rickard"), Atom("robb"))])
def test_final_4_7(): print(f"\nProgram: {list2str(pappend)}") g = [append (Variable("X"), Variable("Y"), \ (cons (Number("1"), (cons (Number("2"), (cons (Number("3"), nil)))))))] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(pappend, g) print(f"Solution: {list2str(g_)}") assert ( g_ == [append (nil, (cons (Number("1"), (cons (Number("2"), (cons (Number("3"), nil)))))), \ (cons (Number("1"), (cons (Number("2"), (cons (Number("3"), nil)))))))] or g_ == [append ((cons (Number("1"), nil)), (cons (Number("2"), (cons (Number("3"), nil)))), \ (cons (Number("1"), (cons (Number("2"), (cons (Number("3"), nil)))))))] or g_ == [append ((cons (Number("1"), (cons (Number("2"), nil)))), (cons (Number("3"), nil)), \ (cons (Number("1"), (cons (Number("2"), (cons (Number("3"), nil)))))))] or g_ == [append ((cons (Number("1"), (cons (Number("2"), (cons (Number("3"), nil)))))), nil, \ (cons (Number("1"), (cons (Number("2"), (cons (Number("3"), nil)))))))] )
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_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_challenge_3(): # Tests choice points g = [ancestor(Variable("X"), Atom("robb"))] print(f"Goal: {list2str(g)}") g_ = interpreter.det_query(pstark, g) assert (len(g_) == 2) g1, g2 = g_[0], g_[1] print(f"Solution: {list2str(g1)}") print(f"Solution: {list2str(g2)}") assert (g1 == [ancestor(Atom("ned"), Atom("robb"))]) assert (g2 == [ancestor(Atom("rickard"), Atom("robb"))])
def test_final_3_2(): t = Variable("Y") t_ = Variable("X") u = {Variable("X"): Variable("Y")} u_ = {Variable("Y"): Variable("X")} assert (interpreter.unify(t, t_) == u or interpreter.unify(t, t_) == u_)
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_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_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_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_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 test_occurs_check(self): v = Variable("E") t = Function("cons", [Atom("a"), Atom("b"), Variable("E")]) #assert (self.occurs_check(v, t))
def test_final_3_5(): t = Number("0") t_ = Variable("Y") u = {Variable("Y"): Number("0")} assert (interpreter.unify(t, t_) == u)
def test_final_1_2(): assert ((interpreter.variables_of_term( Function("p", [Variable("X"), Variable("Y"), Atom("a")]))) == set([Variable("X"), Variable("Y")]))
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_)}")
assert (g_ == [Function("f", [Atom("a"), Atom("b")])]) print(f"Solution: {list2str(g_)}") # Test on the House Stark program def ancestor(x, y): return Function("ancestor", [x, y]) def father(x, y): return Function("father", [x, y]) def father_consts(x, y): return father(Atom(x), Atom(y)) f1 = Rule(father_consts("rickard", "ned"), RuleBody([])) f2 = Rule(father_consts("ned", "robb"), RuleBody([])) r1 = Rule(ancestor(Variable("X"), Variable("Y")), RuleBody([father(Variable("X"), Variable("Y"))])) r2 = Rule (ancestor (Variable ("X"), Variable ("Y")), \ RuleBody([father (Variable ("X"), Variable ("Z")), ancestor (Variable ("Z"), Variable ("Y"))])) pstark = [f1, f2, r1, r2] def test_final_4_4(): print(f"\nProgram: {list2str(pstark)}") g = [ancestor(Atom("rickard"), Atom("ned"))] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(pstark, g) print(f"Solution: {list2str(g_)}") assert (g_ == [ancestor(Atom("rickard"), Atom("ned"))]) def test_final_4_5(): g = [ancestor(Atom("rickard"), Atom("robb"))]
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 test_final_3_3(): t = Variable("Y") t_ = Variable("Y") assert (interpreter.unify(t, t_) == {})
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 variable(self, value): return Variable(value.value)
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_)