Example #1
0
    def test_loop(self):
        e = get_engine("""
            f(X) :- h(X, _).
            f(50).
            h(0, _).
            h(X, Y) :- Y is X - 1, h(Y, _).
        """)
        num = term.Number(50)

        def main(n):
            e.heap.reset()
            if n == 0:
                e.call(term.Term("f", [num]))
                return True
            else:
                return False

        res = main(0)
        assert res
        res = self.timeshift_from_portal(main,
                                         portal.PORTAL, [0],
                                         policy=POLICY,
                                         backendoptimize=True,
                                         inline=0.0)
        assert res
Example #2
0
def impl_between(engine, lower, upper, varorint, continuation):
    if isinstance(varorint, term.Var):
        for i in range(lower, upper):
            oldstate = engine.heap.branch()
            try:
                varorint.unify(term.Number(i), engine.heap)
                return continuation.call(engine, choice_point=True)
            except error.UnificationFailed:
                engine.heap.revert(oldstate)
        varorint.unify(term.Number(upper), engine.heap)
        return continuation.call(engine, choice_point=False)
    else:
        integer = helper.unwrap_int(varorint)
        if not (lower <= integer <= upper):
            raise error.UnificationFailed
    return continuation.call(engine, choice_point=False)
Example #3
0
def impl_arg(engine, first, second, third, continuation):
    if isinstance(second, term.Var):
        error.throw_instantiation_error()
    if isinstance(second, term.Atom):
        raise error.UnificationFailed()
    if not isinstance(second, term.Term):
        error.throw_type_error("compound", second)
    if isinstance(first, term.Var):
        for i in range(len(second.args)):
            arg = second.args[i]
            oldstate = engine.heap.branch()
            try:
                third.unify(arg, engine.heap)
                first.unify(term.Number(i + 1), engine.heap)
                return continuation.call(engine, choice_point=True)
            except error.UnificationFailed:
                engine.heap.revert(oldstate)
        raise error.UnificationFailed()
    elif isinstance(first, term.Number):
        num = first.num
        if num == 0:
            raise error.UnificationFailed
        if num < 0:
            error.throw_domain_error("not_less_than_zero", first)
        if num > len(second.args):
            raise error.UnificationFailed()
        arg = second.args[num - 1]
        third.unify(arg, engine.heap)
    else:
        error.throw_type_error("integer", first)
    return continuation.call(engine, choice_point=False)
Example #4
0
def impl_abolish(engine, predicate):
    from pypy.lang.prolog.builtin import builtins
    name, arity = helper.unwrap_predicate_indicator(predicate)
    if arity < 0:
        error.throw_domain_error("not_less_than_zero", term.Number(arity))
    signature = name + "/" + str(arity)
    if signature in builtins:
        error.throw_permission_error("modify", "static_procedure", predicate)
    if signature in engine.signature2function:
        del engine.signature2function[signature]
Example #5
0
def impl_functor(engine, t, functor, arity):
    if helper.is_atomic(t):
        functor.unify(t, engine.heap)
        arity.unify(term.Number(0), engine.heap)
    elif isinstance(t, term.Term):
        functor.unify(term.Atom(t.name), engine.heap)
        arity.unify(term.Number(len(t.args)), engine.heap)
    elif isinstance(t, term.Var):
        if isinstance(functor, term.Var):
            error.throw_instantiation_error()
        a = helper.unwrap_int(arity)
        if a < 0:
            error.throw_domain_error("not_less_than_zero", arity)
        else:
            functor = helper.ensure_atomic(functor)
            if a == 0:
                t.unify(helper.ensure_atomic(functor), engine.heap)
            else:
                name = helper.unwrap_atom(functor)
                t.unify(term.Term(name, [term.Var() for i in range(a)]),
                        engine.heap)
Example #6
0
def norm_float(obj):
    v = obj.floatval
    if v == int(v):
        return term.Number(int(v))
    else:
        return obj
Example #7
0
def impl_sub_atom(engine, s, before, length, after, sub, continuation):
    # XXX can possibly be optimized
    if isinstance(length, term.Var):
        startlength = 0
        stoplength = len(s) + 1
    else:
        startlength = helper.unwrap_int(length)
        stoplength = startlength + 1
        if startlength < 0:
            startlength = 0
            stoplength = len(s) + 1
    if isinstance(before, term.Var):
        startbefore = 0
        stopbefore = len(s) + 1
    else:
        startbefore = helper.unwrap_int(before)
        stopbefore = startbefore + 1
        if startbefore < 0:
            startbefore = 0
            stopbefore = len(s) + 1
    oldstate = engine.heap.branch()
    if not isinstance(sub, term.Var):
        s1 = helper.unwrap_atom(sub)
        if len(s1) >= stoplength or len(s1) < startlength:
            raise error.UnificationFailed()
        start = startbefore
        while True:
            try:
                try:
                    b = s.find(s1, start, stopbefore + len(s1)) # XXX -1?
                    if b < 0:
                        break
                    start = b + 1
                    before.unify(term.Number(b), engine.heap)
                    after.unify(term.Number(len(s) - len(s1) - b), engine.heap)
                    length.unify(term.Number(len(s1)), engine.heap)
                    return continuation.call(engine, choice_point=True)
                except:
                    engine.heap.revert(oldstate)
                    raise
            except error.UnificationFailed:
                pass
        raise error.UnificationFailed()
    if isinstance(after, term.Var):
        for b in range(startbefore, stopbefore):
            for l in range(startlength, stoplength):
                if l + b > len(s):
                    continue
                try:
                    try:
                        before.unify(term.Number(b), engine.heap)
                        after.unify(term.Number(len(s) - l - b), engine.heap)
                        length.unify(term.Number(l), engine.heap)
                        sub.unify(term.Atom(s[b:b + l]), engine.heap)
                        return continuation.call(engine, choice_point=True)
                    except:
                        engine.heap.revert(oldstate)
                        raise
                except error.UnificationFailed:
                    pass
    else:
        a = helper.unwrap_int(after)
        for l in range(startlength, stoplength):
            b = len(s) - l - a
            assert b >= 0
            if l + b > len(s):
                continue
            try:
                try:
                    before.unify(term.Number(b), engine.heap)
                    after.unify(term.Number(a), engine.heap)
                    length.unify(term.Number(l), engine.heap)
                    sub.unify(term.Atom(s[b:b + l]), engine.heap)
                    return continuation.call(engine, choice_point=True)
                    return None
                except:
                    engine.heap.revert(oldstate)
                    raise
            except error.UnificationFailed:
                pass
    raise error.UnificationFailed()
Example #8
0
def impl_atom_length(engine, s, length):
    if not (isinstance(length, term.Var) or isinstance(length, term.Number)):
        error.throw_type_error("integer", length)
    term.Number(len(s)).unify(length, engine.heap)