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_op_formatting(): f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False) t = parse_query_term("'+'(1, 2).") assert f.format(t) == "1+2" t = parse_query_term("'+'(1, *(3, 2)).") assert f.format(t) == "1+3*2" t = parse_query_term("'*'(1, *(3, 2)).") assert f.format(t) == "1*(3*2)"
def test_op_formatting(): f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False) t = parse_query_term("'+'(1, 2).") assert f.format(t) == "1+2" t = parse_query_term("'+'(1, *(3, 2)).") assert f.format(t) == "1+3*2" t = parse_query_term("'*'(1, *(3, 2)).") assert f.format(t) == "1*(3*2)"
def test_atom_formatting(): f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False) t = parse_query_term("'abc def'.") assert f.format(t) == "abc def" f = formatting.TermFormatter(Engine(), quoted=True, ignore_ops=False) t = parse_query_term("'abc def'.") assert f.format(t) == "'abc def'" t = parse_query_term("abc.") assert f.format(t) == "abc"
def test_atom_formatting(): f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False) t = parse_query_term("'abc def'.") assert f.format(t) == "abc def" f = formatting.TermFormatter(Engine(), quoted=True, ignore_ops=False) t = parse_query_term("'abc def'.") assert f.format(t) == "'abc def'" t = parse_query_term("abc.") assert f.format(t) == "abc"
def test_indexing(): # this test is quite a lot faster if indexing works properly. hrmrm e = get_engine("g(a, b, c, d, e, f, g, h, i, j, k, l). " + "".join(["f(%s, g(%s)) :- g(A, B, C, D, E, F, G, H, I ,J, K, l). " % (chr(i), chr(i + 1)) for i in range(97, 122)])) t = parse_query_term("f(x, g(y)).") for i in range(200): e.run_query_in_current(t) t = parse_query_term("f(x, g(y, a)).") for i in range(200): py.test.raises(UnificationFailed, e.run_query_in_current, t)
def test_indexing(): # this test is quite a lot faster if indexing works properly. hrmrm e = get_engine("g(a, b, c, d, e, f, g, h, i, j, k, l). " + "".join([ "f(%s, g(%s)) :- g(A, B, C, D, E, F, G, H, I ,J, K, l). " % (chr(i), chr(i + 1)) for i in range(97, 122) ])) t = parse_query_term("f(x, g(y)).") for i in range(200): e.run_query_in_current(t) t = parse_query_term("f(x, g(y, a)).") for i in range(200): py.test.raises(UnificationFailed, e.run_query_in_current, t)
def test_lists(): e = get_engine(""" nrev([],[]). nrev([X|Y],Z) :- nrev(Y,Z1), append(Z1,[X],Z). append([],L,L). append([X|Y],L,[X|Z]) :- append(Y,L,Z). """) e.run_query_in_current(parse_query_term("append(%s, %s, X)." % (range(30), range(10)))) e.run_query_in_current(parse_query_term("nrev(%s, X)." % (range(15), ))) e.run_query_in_current(parse_query_term("nrev(%s, %s)." % (range(8), range(7, -1, -1))))
def test_list(): f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False) t = parse_query_term("[1, 2, 3, 4, 5 | X].") assert f.format(t) == "[1, 2, 3, 4, 5|_G0]" t = parse_query_term("[a, b, 'A$%%$$'|[]].") assert f.format(t) == "[a, b, A$%%$$]" t = parse_query_term("'.'(a, b, c).") assert f.format(t) == ".(a, b, c)" X = BindingVar() a = Callable.build('a') t = Callable.build(".", [a, X]) X.binding = Callable.build(".", [a, Callable.build("[]")]) assert f.format(t) == "[a, a]"
def test_lists(): e = get_engine(""" nrev([],[]). nrev([X|Y],Z) :- nrev(Y,Z1), append(Z1,[X],Z). append([],L,L). append([X|Y],L,[X|Z]) :- append(Y,L,Z). """) e.run_query_in_current( parse_query_term("append(%s, %s, X)." % (range(30), range(10)))) e.run_query_in_current(parse_query_term("nrev(%s, X)." % (range(15), ))) e.run_query_in_current( parse_query_term("nrev(%s, %s)." % (range(8), range(7, -1, -1))))
def test_list(): f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False) t = parse_query_term("[1, 2, 3, 4, 5 | X].") assert f.format(t) == "[1, 2, 3, 4, 5|_G0]" t = parse_query_term("[a, b, 'A$%%$$'|[]].") assert f.format(t) == "[a, b, A$%%$$]" t = parse_query_term("'.'(a, b, c).") assert f.format(t) == ".(a, b, c)" X = BindingVar() a = Callable.build('a') t = Callable.build(".", [a, X]) X.binding = Callable.build(".", [a, Callable.build("[]")]) assert f.format(t) == "[a, a]"
def test_and(): e = get_engine(""" g(a, a). g(a, b). g(b, c). f(X, Z) :- g(X, Y), g(Y, Z). """) e.run_query_in_current(parse_query_term("f(a, c).")) t, vars = get_query_and_vars("f(X, c).") e.run_query_in_current(t) assert vars['X'].dereference(None).name() == "a"
def test_and(): e = get_engine(""" g(a, a). g(a, b). g(b, c). f(X, Z) :- g(X, Y), g(Y, Z). """) e.run_query_in_current(parse_query_term("f(a, c).")) t, vars = get_query_and_vars("f(X, c).") e.run_query_in_current(t) assert vars['X'].dereference(None).name()== "a"
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_cut_not_reached(): class CheckContinuation(Continuation): def __init__(self): self.nextcont = None self.module = e.modulewrapper.user_module def is_done(self): return False def activate(self, fcont, heap): assert fcont.is_done() return DoneSuccessContinuation(e), DoneFailureContinuation(e), heap e = get_engine(""" g(X, Y) :- X > 0, !, Y = a. g(_, b). """) e.run_query_in_current(parse_query_term("g(-1, Y), Y == b, g(1, Z), Z == a."), CheckContinuation())
def test_cut_not_reached(): class CheckContinuation(Continuation): def __init__(self): self.nextcont = None self.module = e.modulewrapper.user_module def is_done(self): return False def activate(self, fcont, heap): assert fcont.is_done() return DoneSuccessContinuation(e), DoneFailureContinuation(e), heap e = get_engine(""" g(X, Y) :- X > 0, !, Y = a. g(_, b). """) e.run_query_in_current( parse_query_term("g(-1, Y), Y == b, g(1, Z), Z == a."), CheckContinuation())
def impl_read(engine, heap, stream, obj): from prolog.interpreter.parsing import parse_query_term src = read_till_next_dot(stream) parsed = parse_query_term(src) obj.unify(parsed, heap)
def test_append(self): e = get_engine(""" :- use_module(mod1). app([], X, X). app([H | T1], T2, [H | T3]) :- app(T1, T2, T3). loop(0, []). loop(X, [H|T]) :- X > 0, X0 is X - 1, loop(X0, T). loop1(0, []). loop1(N, [H|T]) :- N > 0, N1 is N - 1, !, loop1(N1, T). loop1(N, [H|T]) :- N > 0, N1 is N - 1, loop1(N1, T). nrev([],[]). nrev([X|Y],Z) :- nrev(Y,Z1), app(Z1,[X],Z). run(X) :- solve([X]). solve([]). solve([A | T]) :- my_pred(A, T, T1), solve(T1). my_pred(app([], X, X), T, T). my_pred(app([H | T1], T2, [H | T3]), T, [app(T1, T2, T3) | T]). my_pred(nrev([], []), T, T). my_pred(nrev([X | Y], Z), Rest, [nrev(Y, Z1), app(Z1, [X], Z) | Rest]). add1(X, X1) :- X1 is X + 1. map(_, [], []). map(Pred, [H1 | T1], [H2 | T2]) :- C =.. [Pred, H1, H2], call(C), map(Pred, T1, T2). partition([],_,[],[]). partition([X|L],Y,[X|L1],L2) :- X =< Y, !, partition(L,Y,L1,L2). partition([X|L],Y,L1,[X|L2]) :- partition(L,Y,L1,L2). loop_mod(0). loop_mod(N) :- N > 0, f(N, N1), loop_mod(N1). loop_when(0). loop_when(N) :- N > 0, when(nonvar(X), loop_when(X)), X is N - 1. freeze_list(Num, Millis) :- make_varlist(Num, List), statistics(walltime, [T1, _]), freeze_list_inner(List), statistics(walltime, [T2, _]), Millis is T2 - T1. freeze_list_inner(List) :- freeze_list_inner_2(List), melt_list(List). freeze_list_inner_2([]). freeze_list_inner_2([H|Rest]) :- freeze(H, true), freeze_list_inner_2(Rest). melt_list([]). melt_list([H|Rest]) :- H = 1, melt_list(Rest). make_varlist(0, []). make_varlist(Num, [_|R]) :- Num > 0, Num1 is Num - 1, make_varlist(Num1, R). when_ground_list(Num, Millis) :- make_varlist(Num, List), statistics(walltime, [T1, _]), when(ground(List), Z = 1), when_ground_list_inner(List), statistics(walltime, [T2, _]), Z == 1, Millis is T2 - T1. when_ground_list_inner([]). when_ground_list_inner([H|R]) :- H = a, when_ground_list_inner(R). """, load_system=True, mod1=""" :- module(mod1, [f/2]). f(N, N1) :- mod2:g(N, N1). """, mod2=""" :- module(mod2, [g/2]). g(N, N1) :- N1 is N - 1. """) t1 = parse_query_term( "app([1, 2, 3, 4, 5, 6], [8, 9], X), X == [1, 2, 3, 4, 5, 6, 8, 9]." ) #t2 = parse_query_term("loop_when(100).") t2 = parse_query_term("freeze_list(15, T).") t3 = parse_query_term( "nrev([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], X), X == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]." ) t4 = parse_query_term( "run(app([1, 2, 3, 4, 5, 6, 7], [8, 9], X)), X == [1, 2, 3, 4, 5, 6, 7, 8, 9]." ) t5 = parse_query_term( "map(add1, [1, 2, 3, 4, 5, 6, 7], X), X == [2, 3, 4, 5, 6, 7, 8].") t6 = parse_query_term( "partition([6, 6, 6, 6, 6, 6, 66, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 106, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 10, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 10], 5, X, Y)." ) t7 = parse_query_term( "findall(X+Y, app([X|_], [Y|_], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), L)." ) def interp_w(c): jitdriver.set_param("inlining", True) if c == 1: t = t1 elif c == 2: t = t2 elif c == 3: t = t3 elif c == 4: t = t4 elif c == 5: t = t5 elif c == 6: t = t6 elif c == 7: t = t7 else: raise ValueError e.run(t, e.modulewrapper.user_module) # XXX #interp_w(2) self.meta_interp(interp_w, [2], listcomp=True, backendopt=True, listops=True)
def test_append(self): e = get_engine(""" :- use_module(mod1). app([], X, X). app([H | T1], T2, [H | T3]) :- app(T1, T2, T3). loop(0, []). loop(X, [H|T]) :- X > 0, X0 is X - 1, loop(X0, T). loop1(0, []). loop1(N, [H|T]) :- N > 0, N1 is N - 1, !, loop1(N1, T). loop1(N, [H|T]) :- N > 0, N1 is N - 1, loop1(N1, T). nrev([],[]). nrev([X|Y],Z) :- nrev(Y,Z1), app(Z1,[X],Z). run(X) :- solve([X]). solve([]). solve([A | T]) :- my_pred(A, T, T1), solve(T1). my_pred(app([], X, X), T, T). my_pred(app([H | T1], T2, [H | T3]), T, [app(T1, T2, T3) | T]). my_pred(nrev([], []), T, T). my_pred(nrev([X | Y], Z), Rest, [nrev(Y, Z1), app(Z1, [X], Z) | Rest]). add1(X, X1) :- X1 is X + 1. map(_, [], []). map(Pred, [H1 | T1], [H2 | T2]) :- C =.. [Pred, H1, H2], call(C), map(Pred, T1, T2). partition([],_,[],[]). partition([X|L],Y,[X|L1],L2) :- X =< Y, !, partition(L,Y,L1,L2). partition([X|L],Y,L1,[X|L2]) :- partition(L,Y,L1,L2). loop_mod(0). loop_mod(N) :- N > 0, f(N, N1), loop_mod(N1). loop_when(0). loop_when(N) :- N > 0, when(nonvar(X), loop_when(X)), X is N - 1. freeze_list(Num, Millis) :- make_varlist(Num, List), statistics(walltime, [T1, _]), freeze_list_inner(List), statistics(walltime, [T2, _]), Millis is T2 - T1. freeze_list_inner(List) :- freeze_list_inner_2(List), melt_list(List). freeze_list_inner_2([]). freeze_list_inner_2([H|Rest]) :- freeze(H, true), freeze_list_inner_2(Rest). melt_list([]). melt_list([H|Rest]) :- H = 1, melt_list(Rest). make_varlist(0, []). make_varlist(Num, [_|R]) :- Num > 0, Num1 is Num - 1, make_varlist(Num1, R). when_ground_list(Num, Millis) :- make_varlist(Num, List), statistics(walltime, [T1, _]), when(ground(List), Z = 1), when_ground_list_inner(List), statistics(walltime, [T2, _]), Z == 1, Millis is T2 - T1. when_ground_list_inner([]). when_ground_list_inner([H|R]) :- H = a, when_ground_list_inner(R). """, load_system=True, mod1 = """ :- module(mod1, [f/2]). f(N, N1) :- mod2:g(N, N1). """, mod2 = """ :- module(mod2, [g/2]). g(N, N1) :- N1 is N - 1. """ ) t1 = parse_query_term("app([1, 2, 3, 4, 5, 6], [8, 9], X), X == [1, 2, 3, 4, 5, 6, 8, 9].") #t2 = parse_query_term("loop_when(100).") t2 = parse_query_term("freeze_list(15, T).") t3 = parse_query_term("nrev([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], X), X == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].") t4 = parse_query_term("run(app([1, 2, 3, 4, 5, 6, 7], [8, 9], X)), X == [1, 2, 3, 4, 5, 6, 7, 8, 9].") t5 = parse_query_term("map(add1, [1, 2, 3, 4, 5, 6, 7], X), X == [2, 3, 4, 5, 6, 7, 8].") t6 = parse_query_term("partition([6, 6, 6, 6, 6, 6, 66, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 106, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 10, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 10], 5, X, Y).") t7 = parse_query_term("findall(X+Y, app([X|_], [Y|_], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), L).") def interp_w(c): jitdriver.set_param("inlining", True) if c == 1: t = t1 elif c == 2: t = t2 elif c == 3: t = t3 elif c == 4: t = t4 elif c == 5: t = t5 elif c == 6: t = t6 elif c == 7: t = t7 else: raise ValueError e.run(t, e.modulewrapper.user_module) # XXX #interp_w(2) self.meta_interp(interp_w, [2], listcomp=True, backendopt=True, listops=True)
def impl_read(engine, heap, stream, obj): from prolog.interpreter.parsing import parse_query_term src = read_till_next_dot(stream) parsed = parse_query_term(src) obj.unify(parsed, heap)
def test_numeral(): e = get_engine(""" num(0). num(succ(X)) :- num(X). add(X, 0, X). add(X, succ(Y), Z) :- add(succ(X), Y, Z). mul(X, 0, 0). mul(X, succ(0), X). mul(X, succ(Y), Z) :- mul(X, Y, A), add(A, X, Z). factorial(0, succ(0)). factorial(succ(X), Y) :- factorial(X, Z), mul(Z, succ(X), Y). """) def nstr(n): if n == 0: return "0" return "succ(%s)" % nstr(n - 1) e.run_query_in_current(parse_query_term("num(0).")) e.run_query_in_current(parse_query_term("num(succ(0)).")) t, vars = get_query_and_vars("num(X).") e.run_query_in_current(t) assert vars['X'].dereference(None).num == 0 e.run_query_in_current(parse_query_term("add(0, 0, 0).")) py.test.raises(UnificationFailed, e.run_query_in_current, parse_query_term(""" add(0, 0, succ(0)).""")) e.run_query_in_current( parse_query_term("add(succ(0), succ(0), succ(succ(0))).")) e.run_query_in_current(parse_query_term("mul(succ(0), 0, 0).")) e.run_query_in_current( parse_query_term("mul(succ(succ(0)), succ(0), succ(succ(0))).")) e.run_query_in_current( parse_query_term( "mul(succ(succ(0)), succ(succ(0)), succ(succ(succ(succ(0))))).")) e.run_query_in_current(parse_query_term("factorial(0, succ(0)).")) e.run_query_in_current(parse_query_term("factorial(succ(0), succ(0)).")) e.run_query_in_current( parse_query_term("factorial(%s, %s)." % (nstr(5), nstr(120))))
def test_numeral(): e = get_engine(""" num(0). num(succ(X)) :- num(X). add(X, 0, X). add(X, succ(Y), Z) :- add(succ(X), Y, Z). mul(X, 0, 0). mul(X, succ(0), X). mul(X, succ(Y), Z) :- mul(X, Y, A), add(A, X, Z). factorial(0, succ(0)). factorial(succ(X), Y) :- factorial(X, Z), mul(Z, succ(X), Y). """) def nstr(n): if n == 0: return "0" return "succ(%s)" % nstr(n - 1) e.run_query_in_current(parse_query_term("num(0).")) e.run_query_in_current(parse_query_term("num(succ(0)).")) t, vars = get_query_and_vars("num(X).") e.run_query_in_current(t) assert vars['X'].dereference(None).num == 0 e.run_query_in_current(parse_query_term("add(0, 0, 0).")) py.test.raises(UnificationFailed, e.run_query_in_current, parse_query_term(""" add(0, 0, succ(0)).""")) e.run_query_in_current(parse_query_term("add(succ(0), succ(0), succ(succ(0))).")) e.run_query_in_current(parse_query_term("mul(succ(0), 0, 0).")) e.run_query_in_current(parse_query_term("mul(succ(succ(0)), succ(0), succ(succ(0))).")) e.run_query_in_current(parse_query_term("mul(succ(succ(0)), succ(succ(0)), succ(succ(succ(succ(0))))).")) e.run_query_in_current(parse_query_term("factorial(0, succ(0)).")) e.run_query_in_current(parse_query_term("factorial(succ(0), succ(0)).")) e.run_query_in_current(parse_query_term("factorial(%s, %s)." % (nstr(5), nstr(120))))