def test_numeral():
    from pypy.lang.prolog.interpreter.term import Term, Atom, Var
    from pypy.lang.prolog.interpreter.engine import Engine
    t = parse_file("""
numeral(null). % end of line comment
numeral(succ(X)) :- numeral(X). % another one

add_numeral(X, null, X).
add_numeral(X, succ(Y), Z) :- add_numeral(succ(X), Y, Z).

greater_than(succ(null), null).
greater_than(succ(X), null) :- greater_than(X, null).
greater_than(succ(X), succ(Y)) :- greater_than(X, Y).
""")
    builder = TermBuilder()
    facts = builder.build(t)
    e = Engine()
    for fact in facts:
        print fact
        e.add_rule(fact)
    assert e.signature2function["add_numeral/3"].rulechain.rule.head.args[1].name == "null"
    four = Term("succ", [Term("succ", [Term("succ",
                [Term("succ", [Atom("null")])])])])
    e.run(parse_query_term("numeral(succ(succ(null)))."))
    term = parse_query_term(
        """add_numeral(succ(succ(null)), succ(succ(null)), X).""")
    e.run(term)
    var = Var(0).getvalue(e.heap)
    print var, e.heap
    # does not raise
    var.unify(four, e.heap)
    term = parse_query_term(
        """greater_than(succ(succ(succ(null))), succ(succ(null))).""")
    e.run(term)
Example #2
0
def test_run():
    e = Engine()
    e.add_rule(Term("f", [Atom.newatom("a"), Atom.newatom("b")]))
    X = Var()
    Y = Var()
    e.add_rule(Term("f", [X, X]))
    e.add_rule(Term(":-", [Term("f", [X, Y]),
                           Term("f", [Y, X])]))
    X = e.heap.newvar()
    e.run(Term("f", [Atom.newatom("b"), X]))
    assert X.dereference(e.heap).name == "b"
    e.run(Term("f", [Atom.newatom("b"), Atom.newatom("a")]))
Example #3
0
def test_term():
    X = Var()
    Y = Var()
    t1 = Term("f", [Atom.newatom("hallo"), X])
    t2 = Term("f", [Y, Atom.newatom("HALLO")])
    heap = Heap()
    print t1, t2
    t1.unify(t2, heap)
    assert X.getvalue(heap).name == "HALLO"
    assert Y.getvalue(heap).name == "hallo"
Example #4
0
 def newvar(self):
     result = Var(self)
     return result
Example #5
0
def test_recursive():
    b = Var()
    heap = Heap()
    b.unify(Term("hallo", [b]), heap)
Example #6
0
def test_unify_var():
    b = Var()
    heap = Heap()
    b.unify(b, heap)
    b.unify(Atom.newatom("hallo"), heap)
    py.test.raises(UnificationFailed, b.unify, Atom.newatom("bye"), heap)
Example #7
0
def test_var():
    b = Var()
    heap = Heap()
    b.unify(Atom.newatom("hallo"), heap)
    assert b.getvalue(heap).name == "hallo"
    a = Var()
    b = Var()
    a.unify(b, heap)
    a.unify(Atom.newatom("hallo"), heap)
    assert a.getvalue(heap).name == "hallo"
    assert b.getvalue(heap).name == "hallo"