Ejemplo n.º 1
0
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()])
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def test_library_dir_single_query():
    e = Engine()
    tempdir = "__temp__"
    create_dir(tempdir)
    try:
        assert_true("add_library_dir('%s')." % tempdir, e)
        assert_true("library_directory('%s')." % tempdir, e)
    finally:
        delete_dir(tempdir)
Ejemplo n.º 4
0
def test_asserta_other_module():
    e = Engine()
    assert_true("asserta(m:f(a)).", e)
    assert len(e.modulewrapper.modules) == 2
    assert_true("m:f(a).", e)
    prolog_raises("existence_error(_, _)", "f(a)", e)
    assert_true("module(m).", e)
    assert_true("f(a).", e)
    assert_true("module(user).", e)
    prolog_raises("existence_error(_, _)", "f(a)", e)
Ejemplo n.º 5
0
def test_importlist_with_not_existing_rule():
    e = Engine()
    m = "mod"
    create_file(m, """
    :- module('%s', [f/1]).
    """ % m)
    try:
        prolog_raises("import_error(mod, 'f/1')", "use_module(%s)" % m, e)
        assert e.modulewrapper.current_module.name == "user"
    finally:
        delete_file(m)
Ejemplo n.º 6
0
def test_load_broken_module_twice():
    e = Engine()
    m = "mod"
    create_file(m, """
    :- module(%s, [f/1]).
    """ % m)
    try:
        prolog_raises("import_error(mod, 'f/1')", "use_module('%s')" % m, e)
        assert_true("use_module('%s')." % m, e)
        assert m in e.modulewrapper.modules
    finally:
        delete_file(m)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
def test_engine_current_module_after_invalid_import():
    m = "m.pl"
    create_file(m, """
    :- module(m, [f(a)]).
    f(a).
    """)
    e = Engine()
    try:
        try:  # XXX strange, prolog_raises does not catch the error
            prolog_raises("type_error(_, _)", "use_module(m)", e)
        except UncaughtError:
            pass
        assert e.modulewrapper.current_module.name == "user"
    finally:
        delete_file(m)
Ejemplo n.º 9
0
def test_add_library_twice():
    e = Engine()
    lib1 = "__lib1__"
    lib2 = "__lib2__"
    create_dir(lib1)
    create_dir(lib2)
    try:
        assert_true("add_library_dir('%s')." % lib1, e)
        assert len(e.modulewrapper.libs) == 1
        assert_true("add_library_dir('%s')." % lib1, e)
        assert len(e.modulewrapper.libs) == 1
        assert_true("add_library_dir('%s')." % lib2, e)
        assert len(e.modulewrapper.libs) == 2
    finally:
        delete_dir(lib1)
        delete_dir(lib2)
Ejemplo n.º 10
0
def test_module_assert_retract():
    e = Engine()
    assert_true("module(m).", e)
    assert_true("assert(x).", e)
    assert_true("asserta(y).", e)
    assert_true("x, y.", e)
    assert_true("module(user).", e)
    assert_false("retract(x).", e)
    assert_false("retract(y).", e)
    assert_true("assert(x).", e)
    assert_true("x.", e)
    assert_true("module(m).", e)
    assert_true("retract(x).", e)
    prolog_raises("existence_error(X, Y)", "x", e)
    assert_true("module(user).", e)
    assert_true("x.", e)
Ejemplo n.º 11
0
def test_prefix_module_in_other_directory():
    d = "__dir__"
    create_dir(d)
    m = "mod"
    create_file("%s/%s" % (d, m), """
    :- module(%s, [f/1]).

    f(a).
    """ % m)

    e = Engine()
    try:
        assert_true("use_module('%s/%s')." % (d, m), e)
        assert_true("current_module(%s)." % m, e)
        assert_true("%s:f(X), X == a." % m, e)
    finally:
        delete_dir(d)
Ejemplo n.º 12
0
def test_add_library_dir():
    e = Engine()
    m = e.modulewrapper
    assert m.libs == []
    prolog_raises("existence_error(X, Y)", "add_library_dir('does_not_exist')",
                  e)

    lib1 = "__lib1__"
    lib2 = "__lib2__"
    create_dir(lib1)
    create_dir(lib2)

    try:
        assert_true("add_library_dir('%s')." % lib1, e)
        assert_true("add_library_dir('%s')." % lib2, e)
        assert len(m.libs) == 2
    finally:
        delete_dir(lib1)
        delete_dir(lib2)
Ejemplo n.º 13
0
def test_run():
    e = Engine()
    e.add_rule(Callable.build("f", [Callable.build("a"), Callable.build("b")]))
    X = BindingVar()
    Y = BindingVar()
    c = Callable.build("f", [X, X])
    e.add_rule(c)
    c2 = Callable.build(":-", [Callable.build("f", [X, Y]),
                           Callable.build("f", [Y, X])])
    e.add_rule(c2)
    hp = Heap()
    X = hp.newvar()
    c3 = Callable.build("f", [Callable.build("b"), X])
    e.run_query_in_current(c3)
    assert X.dereference(hp).name()== "b"
    query = Callable.build("f", [Callable.build("b"), Callable.build("a")]) 
    e.run_query_in_current(query)
Ejemplo n.º 14
0
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)