def test_cyclic_term(): h = Heap() X = h.newvar() t = Callable.build("f", [X], heap=h) t.unify(X, h) Y = h.newvar() t2 = Callable.build("f", [Y], heap=h) t2.unify(Y, h) t.unify(t2, h) # does not crash X.unify(Y, h) # does not crash
def test_cyclic_term(): h = Heap() X = h.newvar() t = Callable.build("f", [X], heap=h) t.unify(X, h) Y = h.newvar() t2 = Callable.build("f", [Y], heap=h) t2.unify(Y, h) t.unify(t2, h) # does not crash X.unify(Y, h) # does not crash
def test_enumerate_vars_of_bound_var(): from prolog.interpreter.memo import EnumerationMemo h = Heap() X = h.newvar() X.setvalue(Callable.build("a"), h) t1 = Callable.build("f", [X]) memo = EnumerationMemo() t2 = t1.enumerate_vars(memo) assert is_term(t2) assert t2.signature().eq(t1.signature()) assert t1.argument_at(0).dereference(None) is t2.argument_at(0)
def test_enumerate_vars_of_bound_var(): from prolog.interpreter.memo import EnumerationMemo h = Heap() X = h.newvar() X.setvalue(Callable.build("a"), h) t1 = Callable.build("f", [X]) memo = EnumerationMemo() t2 = t1.enumerate_vars(memo) assert is_term(t2) assert t2.signature().eq(t1.signature()) assert t1.argument_at(0).dereference(None) is t2.argument_at(0)
def test_callable_build_removes_unneeded_vars(): h1 = Heap() v1 = h1.newvar() v1.binding = 1 h2 = h1.branch() v2 = h2.newvar() v2.binding = 2 t = Callable.build("hello", [v1, v2], heap=h2) assert t.argument_at(0) is v1 assert t.argument_at(1) == 2
def test_callable_build_removes_unneeded_vars(): h1 = Heap() v1 = h1.newvar() v1.binding = 1 h2 = h1.branch() v2 = h2.newvar() v2.binding = 2 t = Callable.build("hello", [v1, v2], heap=h2) assert t.argument_at(0) is v1 assert t.argument_at(1) == 2
def test_run(): e = Engine() e.add_rule(Callable.build("f", [Callable.build("a"), Callable.build("b")])) X = BindingVar() Y = BindingVar() c = Callable.build("f", [X, X]) e.add_rule(c) c2 = Callable.build(":-", [Callable.build("f", [X, Y]), Callable.build("f", [Y, X])]) e.add_rule(c2) hp = Heap() X = hp.newvar() c3 = Callable.build("f", [Callable.build("b"), X]) e.run_query_in_current(c3) assert X.dereference(hp).name()== "b" query = Callable.build("f", [Callable.build("b"), Callable.build("a")]) e.run_query_in_current(query)
def test_run(): e = Engine() e.add_rule(Callable.build("f", [Callable.build("a"), Callable.build("b")])) X = BindingVar() Y = BindingVar() c = Callable.build("f", [X, X]) e.add_rule(c) c2 = Callable.build( ":-", [Callable.build("f", [X, Y]), Callable.build("f", [Y, X])]) e.add_rule(c2) hp = Heap() X = hp.newvar() c3 = Callable.build("f", [Callable.build("b"), X]) e.run_query_in_current(c3) assert X.dereference(hp).name() == "b" query = Callable.build("f", [Callable.build("b"), Callable.build("a")]) e.run_query_in_current(query)