# 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
    e = get_engine("""
        f(x). f(y). f(z).
        g(a). g(b). g(c).
        h(d). h(e). h(f).
        f(X, Y, Z) :- f(X), g(Y), h(Z).
    """)
    heaps = collect_all(e, "f(X, Y, Z).")
    assert len(heaps) == 27  

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

# 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

    e = get_engine("""
        f(x). f(y). f(z).
        g(a). g(b). g(c).
        h(d). h(e). h(f).
        f(X, Y, Z) :- f(X), g(Y), h(Z).
    """)
    heaps = collect_all(e, "f(X, Y, Z).")
    assert len(heaps) == 27  

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)."))