# Path: prolog/interpreter/parsing.py
# def parse_query_term(s):
#     return get_query_and_vars(s)[0]
# 
# def get_engine(source, create_files=False, load_system=False, **modules):
#     from prolog.interpreter.continuation import Engine
#     from prolog.interpreter.test.tool import create_file, delete_file
#     e = Engine(load_system)
#     for name, module in modules.iteritems():
#         if create_files:
#             create_file(name, module)
#         else:
#             e.runstring(module)
#     try:
#         e.modulewrapper.current_module = e.modulewrapper.user_module
#         e.runstring(source)
#     finally:
#         if create_files:
#             for name in modules.keys():
#                 delete_file(name)
#     return e
# 
# Path: prolog/interpreter/parsing.py
# def get_query_and_vars(s):
#     tokens = lexer.tokenize(s, eof=True)
#     s = parser_query.parse(tokens, lazy=False)
#     builder = TermBuilder()
#     query = builder.build_query(s)
#     return query, builder.varname_to_var
# 
# Path: prolog/interpreter/error.py
# class UnificationFailed(PrologError):
#     pass
# 
# Path: prolog/interpreter/test/tool.py
# def collect_all(engine, s):
#     terms, vars = engine.parse(s)
#     term, = terms
#     collector = CollectAllContinuation(engine.modulewrapper.user_module, vars)
#     py.test.raises(UnificationFailed, engine.run_query, term,
#             engine.modulewrapper.current_module, collector)
#     return collector.heaps
# 
# def assert_true(query, e=None):
#     if e is None:
#         e = Engine()
#     terms, vars = e.parse(query)
#     term, = terms
#     e.run_query_in_current(term)
#     return dict([(name, var.dereference(None))
#                      for name, var in vars.iteritems()])
# 
# def assert_false(query, e=None):
#     if e is None:
#         e = Engine()
#     term = e.parse(query)[0][0]
#     py.test.raises(UnificationFailed, e.run_query_in_current, term)


import py
from prolog.interpreter.continuation import *
from prolog.interpreter.parsing import parse_query_term, get_engine
from prolog.interpreter.parsing import get_query_and_vars
from prolog.interpreter.error import UnificationFailed
from prolog.interpreter.test.tool import collect_all, assert_true, assert_false
    from prolog.interpreter.term import BindingVar, Atom, Term
                         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_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_indexing2():
    e = get_engine("""
        mother(o, j).
        mother(o, m).
        mother(o, b).

        sibling(X, Y) :- mother(Z, X), mother(Z, Y).
    """)

# Path: prolog/interpreter/parsing.py
# def parse_query_term(s):
#     return get_query_and_vars(s)[0]
# 
# def get_engine(source, create_files=False, load_system=False, **modules):
#     from prolog.interpreter.continuation import Engine
#     from prolog.interpreter.test.tool import create_file, delete_file
#     e = Engine(load_system)
#     for name, module in modules.iteritems():
#         if create_files:
#             create_file(name, module)
#         else:
#             e.runstring(module)
#     try:
#         e.modulewrapper.current_module = e.modulewrapper.user_module
#         e.runstring(source)
#     finally:
#         if create_files:
#             for name in modules.keys():
#                 delete_file(name)
#     return e
# 
# Path: prolog/interpreter/parsing.py
# def get_query_and_vars(s):
#     tokens = lexer.tokenize(s, eof=True)
#     s = parser_query.parse(tokens, lazy=False)
#     builder = TermBuilder()
#     query = builder.build_query(s)
#     return query, builder.varname_to_var
# 
# Path: prolog/interpreter/error.py
# class UnificationFailed(PrologError):
#     pass
# 
# Path: prolog/interpreter/test/tool.py
# def collect_all(engine, s):
#     terms, vars = engine.parse(s)
#     term, = terms
#     collector = CollectAllContinuation(engine.modulewrapper.user_module, vars)
#     py.test.raises(UnificationFailed, engine.run_query, term,
#             engine.modulewrapper.current_module, collector)
#     return collector.heaps
# 
# def assert_true(query, e=None):
#     if e is None:
#         e = Engine()
#     terms, vars = e.parse(query)
#     term, = terms
#     e.run_query_in_current(term)
#     return dict([(name, var.dereference(None))
#                      for name, var in vars.iteritems()])
# 
# def assert_false(query, e=None):
#     if e is None:
#         e = Engine()
#     term = e.parse(query)[0][0]
#     py.test.raises(UnificationFailed, e.run_query_in_current, term)


# Path: prolog/interpreter/test/test_continuation.py
import py
from prolog.interpreter.continuation import *
from prolog.interpreter.parsing import parse_query_term, get_engine
from prolog.interpreter.parsing import get_query_and_vars
from prolog.interpreter.error import UnificationFailed
from prolog.interpreter.test.tool import collect_all, assert_true, assert_false
    from prolog.interpreter.term import BindingVar, Atom, Term

                         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_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_indexing2():
    e = get_engine("""
        mother(o, j).
        mother(o, m).
        mother(o, b).

        sibling(X, Y) :- mother(Z, X), mother(Z, Y).
    """)

    heaps = collect_all(e, "sibling(m, X).")