Пример #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.")
Пример #2
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].args[1].num == -1
    assert facts[1].args[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.
    """)
Пример #3
0
def test_cut():
    t = parse_file("""
        g(X, /* this is some comment */
        Y) :- g(X), !, h(Y).
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Пример #4
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)
Пример #5
0
def test_simple():
    t = parse_file("""
h(X, Y, Z) :- -Y = Z.
""")
    builder = TermBuilder()
    facts = builder.build(t)
    assert len(facts) == 1
Пример #6
0
def test_numeral():
    from pypy.lang.prolog.interpreter.term import Term, Atom, Var
    from pypy.lang.prolog.interpreter.engine 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()
    for fact in facts:
        print fact
        e.add_rule(fact)
    assert e.signature2function["add_numeral/3"].rulechain.rule.head.args[1].name == "null"
    four = Term("succ", [Term("succ", [Term("succ",
                [Term("succ", [Atom("null")])])])])
    e.run(parse_query_term("numeral(succ(succ(null)))."))
    term = parse_query_term(
        """add_numeral(succ(succ(null)), succ(succ(null)), X).""")
    e.run(term)
    var = Var(0).getvalue(e.heap)
    print var, e.heap
    # does not raise
    var.unify(four, e.heap)
    term = parse_query_term(
        """greater_than(succ(succ(succ(null))), succ(succ(null))).""")
    e.run(term)
Пример #7
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
 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
Пример #9
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)
Пример #10
0
def test_noparam():
    t = parse_file("""
        test.
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Пример #11
0
def test_quoted_atoms():
    t = parse_file("""
        g('ASa0%!!231@~!@#%', a, []). /* /* /* * * * / a mean comment */
    """)
    builder = TermBuilder()
    facts = builder.build(t)
Пример #12
0
 def runstring(self, s):
     from pypy.lang.prolog.interpreter.parsing import parse_file
     trees = parse_file(s, self.parser, Engine._build_and_run, self)
Пример #13
0
 def parse(self, s):
     from pypy.lang.prolog.interpreter.parsing import parse_file, TermBuilder, lexer
     builder = TermBuilder()
     trees = parse_file(s, self.parser)
     terms = builder.build_many(trees)
     return terms, builder.varname_to_var
Пример #14
0
 def runstring(self, s):
     from pypy.lang.prolog.interpreter.parsing import parse_file
     trees = parse_file(s, self.parser, Engine._build_and_run, self)
Пример #15
0
 def parse(self, s):
     from pypy.lang.prolog.interpreter.parsing import parse_file, TermBuilder, lexer
     builder = TermBuilder()
     trees = parse_file(s, self.parser)
     terms = builder.build_many(trees)
     return terms, builder.varname_to_var
def parse(s):
    from pypy.lang.prolog.interpreter.parsing import parse_file
    lines = Lines()
    trees = parse_file(s, lines.parser, _build_and_run, lines)
    return lines
Пример #17
0
def parse(s):
    from pypy.lang.prolog.interpreter.parsing import parse_file
    lines = Lines()
    trees = parse_file(s, lines.parser, _build_and_run, lines)
    return lines