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)
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()