# 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/term.py
# class Number(Numeric):#, UnboxedValue):
#     TYPE_STANDARD_ORDER = 1
#     __slots__ = ("num", )
#     _immutable_fields_ = ["num"]
# 
#     def __init__(self, val):
#         self.num = val
# 
#     @specialize.arg(3)
#     def basic_unify(self, other, heap, occurs_check):
#         if isinstance(other, Number) and other.num == self.num:
#             return
#         raise UnificationFailed
# 
#     def __str__(self):
#         return repr(self.num)
#     
#     def __repr__(self):
#         return "Number(%r)" % (self.num, )
#     
#     def cmp_standard_order(self, other, heap):
#         # XXX looks a bit terrible
#         if isinstance(other, Number):
#             return rcmp(self.num, other.num)
#         elif isinstance(other, Float):
#             # int/float mixed are always compared as floats
#             return rcmp(float(self.num), other.floatval)
#         elif isinstance(other, BigInt):
#             return bigint_cmp(rbigint.fromint(self.num), other.value)
#         assert 0
# 
#     def quick_unify_check(self, other):
#         other = other.dereference(None)
#         if isinstance(other, Var):
#             return True
#         return isinstance(other, Number) and other.num == self.num


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 Number
    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).
    i(X) :- partition([6, 6, 6, 6, 6, 6, 6, 1, 5, 1, 5, 7, 9,2,4, 3, 7, 9, 0, 10], 5, [X | _], _).
    """)
    query = Callable.build("i", [Number(1)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))

def test_tak():
    e = get_engine("""
    tak(X,Y,Z,A) :-
            write(tak(X, Y, Z, A)), nl,
            X =< Y, !,
            write('succeeded'), nl,
            Z = A.
    tak(X,Y,Z,A) :-
            % X > Y,
            X1 is X - 1,
            tak(X1,Y,Z,A1),
            Y1 is Y - 1,
            tak(Y1,Z,X,A2),
            Z1 is Z - 1,
            tak(Z1,X,Y,A3),
            tak(A1,A2,A3,A).

    j(1) :- tak(18, 5, 5, _).
    """)

# 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/term.py
# class Number(Numeric):#, UnboxedValue):
#     TYPE_STANDARD_ORDER = 1
#     __slots__ = ("num", )
#     _immutable_fields_ = ["num"]
# 
#     def __init__(self, val):
#         self.num = val
# 
#     @specialize.arg(3)
#     def basic_unify(self, other, heap, occurs_check):
#         if isinstance(other, Number) and other.num == self.num:
#             return
#         raise UnificationFailed
# 
#     def __str__(self):
#         return repr(self.num)
#     
#     def __repr__(self):
#         return "Number(%r)" % (self.num, )
#     
#     def cmp_standard_order(self, other, heap):
#         # XXX looks a bit terrible
#         if isinstance(other, Number):
#             return rcmp(self.num, other.num)
#         elif isinstance(other, Float):
#             # int/float mixed are always compared as floats
#             return rcmp(float(self.num), other.floatval)
#         elif isinstance(other, BigInt):
#             return bigint_cmp(rbigint.fromint(self.num), other.value)
#         assert 0
# 
#     def quick_unify_check(self, other):
#         other = other.dereference(None)
#         if isinstance(other, Var):
#             return True
#         return isinstance(other, Number) and other.num == self.num


# Path: prolog/interpreter/test/test_resource_consumption.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 Number

    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).
    i(X) :- partition([6, 6, 6, 6, 6, 6, 6, 1, 5, 1, 5, 7, 9,2,4, 3, 7, 9, 0, 10], 5, [X | _], _).
    """)
    query = Callable.build("i", [Number(1)])
    e.run_query(query, e.modulewrapper.user_module, CheckContinuation(e))

def test_tak():
    e = get_engine("""
    tak(X,Y,Z,A) :-
            write(tak(X, Y, Z, A)), nl,
            X =< Y, !,
            write('succeeded'), nl,
            Z = A.
    tak(X,Y,Z,A) :-
            % X > Y,
            X1 is X - 1,
            tak(X1,Y,Z,A1),
            Y1 is Y - 1,
            tak(Y1,Z,X,A2),
            Z1 is Z - 1,
            tak(Z1,X,Y,A3),
            tak(A1,A2,A3,A).

    j(1) :- tak(18, 5, 5, _).
    """)

    query = Callable.build("j", [Number(1)])