Esempio n. 1
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)
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)
Esempio n. 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)
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)
Esempio n. 5
0
 def from_option_list(engine, options):
     # XXX add numbervars support
     quoted = False
     max_depth = 0
     ignore_ops = False
     number_vars = False
     for option in options:
         if (not isinstance(option, Term) or len(option.args) != 1):
             error.throw_domain_error('write_option', option)
         arg = option.args[0]
         if option.name == "max_depth":
             try:
                 max_depth = helper.unwrap_int(arg)
             except error.CatchableError:
                 error.throw_domain_error('write_option', option)
         elif (not isinstance(arg, Atom) or
             (arg.name != "true" and arg.name != "false")):
             error.throw_domain_error('write_option', option)
             assert 0, "unreachable"
         elif option.name == "quoted":
             quoted = arg.name == "true"
         elif option.name == "ignore_ops":
             ignore_ops = arg.name == "true"
     return TermFormatter(engine, quoted, max_depth, ignore_ops)
Esempio n. 6
0
 def from_option_list(engine, options):
     # XXX add numbervars support
     quoted = False
     max_depth = 0
     ignore_ops = False
     number_vars = False
     for option in options:
         if (not isinstance(option, Term) or len(option.args) != 1):
             error.throw_domain_error('write_option', option)
         arg = option.args[0]
         if option.name == "max_depth":
             try:
                 max_depth = helper.unwrap_int(arg)
             except error.CatchableError:
                 error.throw_domain_error('write_option', option)
         elif (not isinstance(arg, Atom)
               or (arg.name != "true" and arg.name != "false")):
             error.throw_domain_error('write_option', option)
             assert 0, "unreachable"
         elif option.name == "quoted":
             quoted = arg.name == "true"
         elif option.name == "ignore_ops":
             ignore_ops = arg.name == "true"
     return TermFormatter(engine, quoted, max_depth, ignore_ops)
Esempio n. 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()