def test_numeral(): from prolog.interpreter.term import Callable, Atom, BindingVar from prolog.interpreter.continuation 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() m = e.modulewrapper for fact in facts: print fact e.add_rule(fact) assert m.modules["user"].lookup(Signature.getsignature("add_numeral", 3)).rulechain.head.argument_at(1).name() == "null" four = Callable.build("succ", [Callable.build("succ", [Callable.build("succ", [Callable.build("succ", [Callable.build("null")])])])]) e.run_query_in_current(parse_query_term("numeral(succ(succ(null))).")) term = parse_query_term( """add_numeral(succ(succ(null)), succ(succ(null)), X).""") e.run_query_in_current(term) hp = Heap() var = BindingVar().dereference(hp) # does not raise var.unify(four, hp) term = parse_query_term( """greater_than(succ(succ(succ(null))), succ(succ(null))).""") e.run_query_in_current(term)
def test_unwrap_list(): a = Callable.build("a") l = unwrap_list(Callable.build(".", [a, Callable.build("[]")])) assert len(l) == 1 assert l[0] is a v1 = BindingVar() a1 = Callable.build("a") l1 = unwrap_list(Callable.build(".", [v1, Callable.build(".", [a1, Callable.build("[]")])])) assert l1 == [v1, a1] empty = Callable.build("[]") v2 = BindingVar() l2 = Callable.build(".", [a, v2]) v2.unify(empty, Heap()) unwrapped = unwrap_list(l2) assert unwrapped == [a] v3 = BindingVar() v4 = BindingVar() b = Callable.build("b") h = Heap() l3 = Callable.build(".", [a, v3]) v3.unify(Callable.build(".", [b, v4]), h) v4.unify(Callable.build("[]"), h) unwrapped2 = unwrap_list(l3) assert unwrapped2 == [a, b]
def test_numeral(): from prolog.interpreter.term import Callable, Atom, BindingVar from prolog.interpreter.continuation 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() m = e.modulewrapper for fact in facts: print fact e.add_rule(fact) assert m.modules["user"].lookup(Signature.getsignature( "add_numeral", 3)).rulechain.head.argument_at(1).name() == "null" four = Callable.build("succ", [ Callable.build("succ", [ Callable.build("succ", [Callable.build("succ", [Callable.build("null")])]) ]) ]) e.run_query_in_current(parse_query_term("numeral(succ(succ(null))).")) term = parse_query_term( """add_numeral(succ(succ(null)), succ(succ(null)), X).""") e.run_query_in_current(term) hp = Heap() var = BindingVar().dereference(hp) # does not raise var.unify(four, hp) term = parse_query_term( """greater_than(succ(succ(succ(null))), succ(succ(null))).""") e.run_query_in_current(term)
def test_var(): b = BindingVar() heap = Heap() b.unify(Callable.build("hallo"), heap) assert b.dereference(heap).name()== "hallo" a = BindingVar() b = BindingVar() a.unify(b, heap) a.unify(Callable.build("hallo"), heap) assert a.dereference(heap).name()== "hallo" assert b.dereference(heap).name()== "hallo"
def test_var(): b = BindingVar() heap = Heap() b.unify(Callable.build("hallo"), heap) assert b.dereference(heap).name() == "hallo" a = BindingVar() b = BindingVar() a.unify(b, heap) a.unify(Callable.build("hallo"), heap) assert a.dereference(heap).name() == "hallo" assert b.dereference(heap).name() == "hallo"
def test_recursive(): b = BindingVar() heap = Heap() b.unify(Callable.build("hallo", [b]), heap)
def test_unify_var(): b = BindingVar() heap = Heap() b.unify(b, heap) b.unify(Callable.build("hallo"), heap) py.test.raises(UnificationFailed, b.unify, Callable.build("bye"), heap)
def test_simple_hooks(): hp = Heap() v = BindingVar() a = AttVar() v.unify(a, hp) assert hp.hook is None v.unify(Number(1), hp) assert hp.hook.attvar == a hp = Heap() v1 = BindingVar() v2 = BindingVar() a1 = AttVar() a2 = AttVar() v1.unify(a1, hp) assert hp.hook is None v2.unify(a2, hp) assert hp.hook is None v1.unify(v2, hp) assert hp.hook.attvar == a1 hp = Heap() v1 = BindingVar() v2 = BindingVar() v3 = BindingVar() a1 = AttVar() a2 = AttVar() a3 = AttVar() v1.unify(a1, hp) v2.unify(a2, hp) v3.unify(a3, hp) v1.unify(v2, hp) v2.unify(v3, hp) assert hp.hook.attvar == a2 assert hp.hook.next.attvar == a1 assert hp.hook.next.next is None hp = Heap() v1 = BindingVar() v2 = BindingVar() a1 = AttVar() a2 = AttVar() v1.unify(a1, hp) v2.unify(a2, hp) assert hp.hook is None v1.unify(v2, hp) assert hp.hook.attvar == a1 v1.unify(Number(1), hp) assert hp.hook.attvar == a2 assert hp.hook.next.attvar == a1 assert hp.hook.next.next is None hp = Heap() v1 = BindingVar() v2 = BindingVar() a1 = AttVar() a2 = AttVar() v1.unify(a1, hp) v2.unify(a2, hp) t1 = Callable.build("f", [v1, v2]) t2 = Callable.build("f", [Atom("a"), Atom("b")]) t1.unify(t2, hp) assert hp.hook.attvar == a2 assert hp.hook.next.attvar == a1 assert hp.hook.next.next is None hp = Heap() v = BindingVar() av = AttVar() v.unify(av, hp) assert hp.hook is None a = Callable.build("a") v.unify(a, hp) assert hp.hook.attvar == av v.unify(a, hp) assert hp.hook.attvar == av assert hp.hook.next is None