def add_rule(self, ruleterm, end=True): module = self.modulewrapper.current_module rule = self.make_rule(ruleterm, module) signature = rule.signature if self.get_builtin(signature): error.throw_permission_error( "modify", "static_procedure", rule.head.get_prolog_signature()) function = module.lookup(signature) function.add_rule(rule, end) return rule
def add_rule(self, ruleterm, end=True): module = self.modulewrapper.current_module rule = self.make_rule(ruleterm, module) signature = rule.signature if self.get_builtin(signature): error.throw_permission_error("modify", "static_procedure", rule.head.get_prolog_signature()) function = module.lookup(signature) #adding to module.functions function.add_rule(rule, end) #adding to rulechain return rule
def unwrap_instream(engine, obj): if isinstance(obj, term.Var): error.throw_instantiation_error() if isinstance(obj, term.Atom): try: stream = engine.streamwrapper.aliases[obj.name()] except KeyError: pass else: if not isinstance(stream, PrologInputStream): error.throw_permission_error("input", "stream", term.Atom(stream.alias)) assert isinstance(stream, PrologInputStream) return stream error.throw_domain_error("stream", obj)
def unwrap_outstream(engine, obj): if isinstance(obj, term.Var): error.throw_instantiation_error() if isinstance(obj, term.Atom): try: stream = engine.streamwrapper.aliases[obj.name()] except KeyError: pass else: if not isinstance(stream, PrologOutputStream): error.throw_permission_error("output", "stream", term.Atom(stream.alias)) assert isinstance(stream, PrologOutputStream) return stream error.throw_domain_error("stream", obj)
def impl_abolish(engine, heap, module, predicate): modname = None if predicate.signature().eq(prefixsig): modname, predicate = unpack_modname_and_predicate(predicate) name, arity = helper.unwrap_predicate_indicator(predicate) if arity < 0: error.throw_domain_error("not_less_than_zero", term.Number(arity)) signature = Signature.getsignature(name, arity) if signature.get_extra("builtin"): error.throw_permission_error("modify", "static_procedure", predicate) if modname is not None: module = engine.modulewrapper.get_module(modname, predicate) try: del module.functions[signature] except KeyError: pass
def impl_abolish(engine, heap, module, predicate): modname = None if predicate.signature().eq(prefixsig): modname, predicate = unpack_modname_and_predicate(predicate) name, arity = helper.unwrap_predicate_indicator(predicate) if arity < 0: error.throw_domain_error("not_less_than_zero", term.Number(arity)) signature = Signature.getsignature(name, arity) if signature.get_extra("builtin"): error.throw_permission_error("modify", "static_procedure", predicate) if modname is not None: module = engine.modulewrapper.get_module(modname, predicate) try: del module.functions[signature] except KeyError: pass
def impl_retract(engine, heap, module, pattern): modname = None if pattern.signature().eq(prefixsig): modname, pattern = unpack_modname_and_predicate(pattern) assert isinstance(pattern, term.Callable) if helper.is_term(pattern) and pattern.signature().eq(implsig): head = helper.ensure_callable(pattern.argument_at(0)) body = helper.ensure_callable(pattern.argument_at(1)) else: head = pattern body = None assert isinstance(head, term.Callable) if head.signature().get_extra("builtin"): error.throw_permission_error("modify", "static_procedure", head.get_prolog_signature()) if modname is None: function = module.lookup(head.signature()) else: function = engine.modulewrapper.get_module(modname, pattern).lookup(head.signature()) if function.rulechain is None: raise error.UnificationFailed rulechain = function.rulechain oldstate = heap.branch() while rulechain: rule = rulechain # standardizing apart try: deleted_body = rule.clone_and_unify_head(heap, head) if body is not None: body.unify(deleted_body, heap) except error.UnificationFailed: oldstate.revert_upto(heap) else: if function.rulechain is rulechain: function.rulechain = rulechain.next else: function.remove(rulechain) break rulechain = rulechain.next else: raise error.UnificationFailed()
def impl_retract(engine, heap, module, pattern): modname = None if pattern.signature().eq(prefixsig): modname, pattern = unpack_modname_and_predicate(pattern) assert isinstance(pattern, term.Callable) if helper.is_term(pattern) and pattern.signature().eq(implsig): head = helper.ensure_callable(pattern.argument_at(0)) body = helper.ensure_callable(pattern.argument_at(1)) else: head = pattern body = None assert isinstance(head, term.Callable) if head.signature().get_extra("builtin"): error.throw_permission_error("modify", "static_procedure", head.get_prolog_signature()) if modname is None: function = module.lookup(head.signature()) else: function = engine.modulewrapper.get_module(modname, pattern).lookup( head.signature()) if function.rulechain is None: raise error.UnificationFailed rulechain = function.rulechain oldstate = heap.branch() while rulechain: rule = rulechain # standardizing apart try: deleted_body = rule.clone_and_unify_head(heap, head) if body is not None: body.unify(deleted_body, heap) except error.UnificationFailed: oldstate.revert_upto(heap) else: if function.rulechain is rulechain: function.rulechain = rulechain.next else: function.remove(rulechain) break rulechain = rulechain.next else: raise error.UnificationFailed()