Beispiel #1
0
def impl_atom_concat(engine, a1, a2, result, continuation):
    if isinstance(a1, term.Var):
        if isinstance(a2, term.Var):
            # nondeterministic splitting of result
            r = helper.convert_to_str(result)
            for i in range(len(r) + 1):
                oldstate = engine.heap.branch()
                try:
                    a1.unify(term.Atom(r[:i]), engine.heap)
                    a2.unify(term.Atom(r[i:]), engine.heap)
                    return continuation.call(engine, choice_point=True)
                except error.UnificationFailed:
                    engine.heap.revert(oldstate)
            raise error.UnificationFailed()
        else:
            s2 = helper.convert_to_str(a2)
            r = helper.convert_to_str(result)
            if r.endswith(s2):
                stop = len(r) - len(s2)
                assert stop > 0
                a1.unify(term.Atom(r[:stop]), engine.heap)
            else:
                raise error.UnificationFailed()
    else:
        s1 = helper.convert_to_str(a1)
        if isinstance(a2, term.Var):
            r = helper.convert_to_str(result)
            if r.startswith(s1):
                a2.unify(term.Atom(r[len(s1):]), engine.heap)
            else:
                raise error.UnificationFailed()
        else:
            s2 = helper.convert_to_str(a2)
            result.unify(term.Atom(s1 + s2), engine.heap)
    return continuation.call(engine, choice_point=False)
Beispiel #2
0
def impl_univ(engine, first, second):
    if not isinstance(first, term.Var):
        if isinstance(first, term.Term):
            l = [term.Atom(first.name)] + first.args
        else:
            l = [first]
        u1 = helper.wrap_list(l)
        if not isinstance(second, term.Var):
            u1.unify(second, engine.heap)
        else:
            u1.unify(second, engine.heap)
    else:
        if isinstance(second, term.Var):
            error.throw_instantiation_error()
        else:
            l = helper.unwrap_list(second)
            head = l[0]
            if not isinstance(head, term.Atom):
                error.throw_type_error("atom", head)
            term.Term(head.name, l[1:]).unify(first, engine.heap)
Beispiel #3
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)
Beispiel #4
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()
Beispiel #5
0
def execute(e, filename):
    e.run(term.Term("consult", [term.Atom(filename)]))