# 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
    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_or_backtrack():
    e = get_engine("""
        a(a).
        b(b).
        g(a, b).
        g(a, a).
        f(X, Y, Z) :- (g(X, Z); g(X, Z); g(Z, Y)), a(Z).
        """)
    t, vars = get_query_and_vars("f(a, b, Z).")
    e.run_query_in_current(t)
    assert vars['Z'].dereference(None).name()== "a"
    f = collect_all(e, "X = 1; X = 2.")
    assert len(f) == 2

def test_backtrack_to_same_choice_point():
    e = get_engine("""

# 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

    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_or_backtrack():
    e = get_engine("""
        a(a).
        b(b).
        g(a, b).
        g(a, a).
        f(X, Y, Z) :- (g(X, Z); g(X, Z); g(Z, Y)), a(Z).
        """)
    t, vars = get_query_and_vars("f(a, b, Z).")
    e.run_query_in_current(t)
    assert vars['Z'].dereference(None).name()== "a"
    f = collect_all(e, "X = 1; X = 2.")
    assert len(f) == 2

def test_backtrack_to_same_choice_point():
    e = get_engine("""

        a(a).