Ejemplo n.º 1
0
def test_chaining():
    t = parse_file("f(X) = X + X + 1 + 2.")
    builder = TermBuilder()
    facts = builder.build(t)
    t = parse_file("f(X) = X + X * 1 + 23 / 13.")
    facts = builder.build(t)
    t = parse_file("-X + 1.")
Ejemplo n.º 2
0
def test_chaining():
    t = parse_file("f(X) = X + X + 1 + 2.")
    builder = TermBuilder()
    facts = builder.build(t)
    t = parse_file("f(X) = X + X * 1 + 23 / 13.")
    facts = builder.build(t)
    t = parse_file("-X + 1.")
Ejemplo n.º 3
0
 def runstring(self, s, file_name=None, similarity=None):
     from prolog.interpreter.parsing import parse_file
     parse_file(s,
                None,
                Engine._build_and_run,
                self,
                file_name=file_name,
                similarity=similarity)
Ejemplo n.º 4
0
def test_number():
    t = parse_file("""
        X = -1.
        Y = -1.345.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 2
    assert facts[0].argument_at(1).num == -1
    assert facts[1].argument_at(1).floatval == -1.345
    t = parse_file("""
        X = -1.
        arg(X, h(a, b, c), b), X = 2.
        arg(X, h(a, b, g(X, b)), g(3, B)), X = 3, B = b.
    """)
Ejemplo n.º 5
0
def test_number():
    t = parse_file("""
        X = -1.
        Y = -1.345.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 2
    assert facts[0].argument_at(1).num == -1
    assert facts[1].argument_at(1).floatval == -1.345
    t = parse_file("""
        X = -1.
        arg(X, h(a, b, c), b), X = 2.
        arg(X, h(a, b, g(X, b)), g(3, B)), X = 3, B = b.
    """)
Ejemplo n.º 6
0
def test_meta_predicate():
    t = parse_file(":- meta_predicate f(:), f(2, '+', '+'), f(:, '-'), a.")
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
    assert facts[0].name() == ":-"
    assert facts[0].argument_at(0).name() == "meta_predicate"
Ejemplo n.º 7
0
def test_meta_predicate():
    t = parse_file(":- meta_predicate f(:), f(2, '+', '+'), f(:, '-'), a.")
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
    assert facts[0].name() == ":-"
    assert facts[0].argument_at(0).name() == "meta_predicate"
Ejemplo n.º 8
0
def test_cut():
    t = parse_file("""
        g(X, /* this is some comment */
        Y) :- g(X), !, h(Y).
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 9
0
def test_quoted_atoms():
    t = parse_file("""
        g('ASa0%!!231@~!@#%', a, []). /* /* /* * * * / a mean comment */
    """)
    builder = TermBuilder()
    fact, = builder.build(t)
    assert fact.argument_at(0).name() == 'ASa0%!!231@~!@#%'
    assert fact.argument_at(1).name() == 'a'
    assert fact.argument_at(2).name() == '[]'
    t = parse_file("""
        'a'.
        a.
    """)
    builder = TermBuilder()
    fact1, fact2, = builder.build(t)
    assert fact1.name() == fact2.name()
Ejemplo n.º 10
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.º 11
0
def test_parenthesis():
    t = parse_file("""
        g(X, Y) :- (g(x, y); g(a, b)), /* this too is a comment
*/ g(x, z).
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 12
0
def test_block():
    t = parse_file(":- block f('-', '?'), f('-', '?', '?'), a.")
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
    assert facts[0].name() == ":-"
    assert facts[0].argument_at(0).name() == "block"
Ejemplo n.º 13
0
def test_cut():
    t = parse_file("""
        g(X, /* this is some comment */
        Y) :- g(X), !, h(Y).
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 14
0
def test_simple():
    t = parse_file("""
h(X, Y, Z) :- -Y = Z.
""")
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
Ejemplo n.º 15
0
def test_block():
    t = parse_file(":- block f('-', '?'), f('-', '?', '?'), a.")
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
    assert facts[0].name() == ":-"
    assert facts[0].argument_at(0).name() == "block"
Ejemplo n.º 16
0
def test_parenthesis():
    t = parse_file("""
        g(X, Y) :- (g(x, y); g(a, b)), /* this too is a comment
*/ g(x, z).
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 17
0
def test_quoted_atoms():
    t = parse_file("""
        g('ASa0%!!231@~!@#%', a, []). /* /* /* * * * / a mean comment */
    """)
    builder = TermBuilder()
    fact, = builder.build(t)
    assert fact.argument_at(0).name()== 'ASa0%!!231@~!@#%'
    assert fact.argument_at(1).name()== 'a'
    assert fact.argument_at(2).name()== '[]'
    t = parse_file("""
        'a'.
        a.
    """)
    builder = TermBuilder()
    fact1, fact2, = builder.build(t)
    assert fact1.name()== fact2.name()
Ejemplo n.º 18
0
def test_simple():
    t = parse_file("""
h(X, Y, Z) :- -Y = Z.
""")
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
Ejemplo n.º 19
0
 def f(x):
     if x:
         s = "a(X, Y, Z)."
     else:
         s = "f(a, X, _, _, X, f(X, 2.455))."
     term = parsing.parse_file(s)
     assert isinstance(term, parsing.Nonterminal)
     return term.symbol
Ejemplo n.º 20
0
def test_list():
    t = parse_file("""
        W = [].
        X = [a, b, c, d, e, f, g, h].
        Y = [a|T].
        Z = [a,b,c|T].
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 21
0
def test_list():
    t = parse_file("""
        W = [].
        X = [a, b, c, d, e, f, g, h].
        Y = [a|T].
        Z = [a,b,c|T].
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 22
0
def test_scientific_notation():
    t = parse_file("""
        X = -1.2e5.
        Y = -1.345e0.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 2
    assert facts[0].argument_at(1).floatval == -1.2e5
    assert facts[1].argument_at(1).floatval == -1.345
Ejemplo n.º 23
0
def test_braces():
    t = parse_file("""
        W = {}.
        X = {}(a, b, c).
        Y = {a, b, c}.
    """)
    builder = TermBuilder()
    facts = builder.build(t)

    t = parse_file("""
        {a, b, c}.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
    assert facts[0].name() == "{}"
    assert facts[0].argument_count() == 1
    assert facts[0].argument_at(0).name() == ","
    assert facts[0].argument_at(0).argument_count() == 2
Ejemplo n.º 24
0
def test_braces():
    t = parse_file("""
        W = {}.
        X = {}(a, b, c).
        Y = {a, b, c}.
    """)
    builder = TermBuilder()
    facts = builder.build(t)

    t = parse_file("""
        {a, b, c}.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
    assert facts[0].name() == "{}"
    assert facts[0].argument_count() == 1
    assert facts[0].argument_at(0).name() == ","
    assert facts[0].argument_at(0).argument_count() == 2
Ejemplo n.º 25
0
def test_scientific_notation():
    t = parse_file("""
        X = -1.2e5.
        Y = -1.345e0.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 2
    assert facts[0].argument_at(1).floatval == -1.2e5
    assert facts[1].argument_at(1).floatval == -1.345
Ejemplo n.º 26
0
def test_block_comment_basic():
    t = parse_file("""
        g(a).
        /*
        a.
        b.
        f(x).
        */
        h(e).
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 2
Ejemplo n.º 27
0
def test_block_comment_basic():
    t = parse_file("""
        g(a).
        /*
        a.
        b.
        f(x).
        */
        h(e).
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 2
Ejemplo n.º 28
0
def test_many_block_comments():
    t = parse_file("""

        a.

        /*
            b.
            c.
            d.
        */

        a2.

        /**********************************************

            d.
            e.

        ********************************************** */

        a3.

        /*
            x. 
            y.
            z.
        * */

        /*
        f.
        **/

        a4.

    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 4
    assert facts[0].name() == "a"
    assert facts[1].name() == "a2"
    assert facts[2].name() == "a3"
    assert facts[3].name() == "a4"
Ejemplo n.º 29
0
def test_many_block_comments():
    t = parse_file("""

        a.

        /*
            b.
            c.
            d.
        */

        a2.

        /**********************************************

            d.
            e.

        ********************************************** */

        a3.

        /*
            x. 
            y.
            z.
        * */

        /*
        f.
        **/

        a4.

    """)
    builder =  TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 4
    assert facts[0].name() == "a"
    assert facts[1].name() == "a2"
    assert facts[2].name() == "a3"
    assert facts[3].name() == "a4"
Ejemplo n.º 30
0
def test_block_comment_stars_and_stripes():
    t = parse_file("""
        the_first_fact.
        /* this is some random stuff ....

            * / * / * / ******************** /*

        */
        some_fact.
        some_other_fact.

        /**************
        
            skjdhfjskdfhskjfd.

        *************/
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 3
Ejemplo n.º 31
0
def test_block_comment_stars_and_stripes():
    t = parse_file("""
        the_first_fact.
        /* this is some random stuff ....

            * / * / * / ******************** /*

        */
        some_fact.
        some_other_fact.

        /**************
        
            skjdhfjskdfhskjfd.

        *************/
    """)
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 3
Ejemplo n.º 32
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.º 33
0
 def runstring(self, s, file_name=None):
     from prolog.interpreter.parsing import parse_file
     parse_file(s, None, Engine._build_and_run, self, file_name=file_name)
Ejemplo n.º 34
0
 def parse(self, s, file_name=None):
     from prolog.interpreter.parsing import parse_file, TermBuilder
     builder = TermBuilder()
     trees = parse_file(s, None, file_name=file_name)
     terms = builder.build_many(trees)
     return terms, builder.varname_to_var
Ejemplo n.º 35
0
def test_noparam():
    t = parse_file("""
        test.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 36
0
def test_noparam():
    t = parse_file("""
        test.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Ejemplo n.º 37
0
 def parse(self, s, file_name=None):
     from prolog.interpreter.parsing import parse_file, TermBuilder
     builder = TermBuilder()
     trees = parse_file(s, None, file_name=file_name)
     terms = builder.build_many(trees)
     return terms, builder.varname_to_var
Ejemplo n.º 38
0
def parse(inp):
    t = parse_file(inp)
    builder = TermBuilder()
    return builder.build(t)
Ejemplo n.º 39
0
def parse(inp):
    t = parse_file(inp)
    builder = TermBuilder()
    return builder.build(t)