def main(n): e.heap.reset() if n == 0: e.call(term.Term("f", [num])) return True else: return False
def main(n): e.heap.reset() if n == 0: e.call(term.Term("h", [X])) return isinstance(X.dereference(e.heap), term.Atom) else: return False
def throw_type_error(valid_type, obj): from pypy.lang.prolog.interpreter import term # valid types are: # atom, atomic, byte, callable, character # evaluable, in_byte, in_character, integer, list # number, predicate_indicator, variable from pypy.lang.prolog.interpreter import term raise CatchableError( term.Term("type_error", [term.Atom.newatom(valid_type), obj]))
def throw_domain_error(valid_domain, obj): from pypy.lang.prolog.interpreter import term # valid domains are: # character_code_list, close_option, flag_value, io_mode, # not_empty_list, not_less_than_zero, operator_priority, # operator_specifier, prolog_flag, read_option, source_sink, # stream, stream_option, stream_or_alias, stream_position, # stream_property, write_option raise CatchableError( term.Term("domain_error", [term.Atom.newatom(valid_domain), obj]))
def throw_permission_error(operation, permission_type, obj): from pypy.lang.prolog.interpreter import term # valid operations are: # access, create, input, modify, open, output, reposition # valid permission_types are: # binary_stream, flag, operator, past_end_of_stream, private_procedure, # static_procedure, source_sink, stream, text_stream. raise CatchableError( term.Term("permission_error", [term.Atom.newatom(operation), term.Atom.newatom(permission_type), obj]))
def test_simple(self): e = get_engine(""" f(x, y). f(a(X), b(b(Y))) :- f(X, Y). """) X = e.heap.newvar() Y = e.heap.newvar() larger = term.Term( "f", [term.Term("a", [X]), term.Term("b", [term.Term("b", [Y])])]) def main(n): e.heap.reset() if n == 0: e.call(term.Term("f", [X, Y])) return isinstance(X.dereference(e.heap), term.Atom) if n == 1: e.call(larger) return isinstance(X.dereference(e.heap), term.Atom) else: return False res = main(0) assert res == True res = main(1) assert res == True res = self.timeshift_from_portal(main, portal.PORTAL, [1], policy=POLICY, backendoptimize=True, inline=0.0) assert res == True res = self.timeshift_from_portal(main, portal.PORTAL, [0], policy=POLICY, backendoptimize=True, inline=0.0) assert res == True
def impl_findall(engine, template, goal, bag): oldstate = engine.heap.branch() collector = FindallContinuation(template) try: engine.call(goal, collector) except error.UnificationFailed: engine.heap.revert(oldstate) result = term.Atom.newatom("[]") for i in range(len(collector.found) - 1, -1, -1): copy = collector.found[i] d = {} copy = copy.copy(engine.heap, d) result = term.Term(".", [copy, result]) bag.unify(result, engine.heap)
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)
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 wrap_list(python_list): curr = emptylist for i in range(len(python_list) - 1, -1, -1): curr = term.Term(".", [python_list[i], curr]) return curr
def throw_existence_error(object_type, obj): from pypy.lang.prolog.interpreter import term # valid types are: # procedure, source_sink, stream raise CatchableError( term.Term("existence_error", [term.Atom.newatom(object_type), obj]))
def __init__(self, errorterm): from pypy.lang.prolog.interpreter import term self.term = term.Term("error", [errorterm])
def execute(e, filename): e.run(term.Term("consult", [term.Atom(filename)]))