Esempio n. 1
0
def impl_abolish(engine, predicate):
    from pypy.lang.prolog.builtin import builtins
    name, arity = helper.unwrap_predicate_indicator(predicate)
    if arity < 0:
        error.throw_domain_error("not_less_than_zero", term.Number(arity))
    signature = name + "/" + str(arity)
    if signature in builtins:
        error.throw_permission_error("modify", "static_procedure", predicate)
    if signature in engine.signature2function:
        del engine.signature2function[signature]
Esempio n. 2
0
def impl_abolish(engine, predicate):
    from pypy.lang.prolog.builtin import builtins
    name, arity = helper.unwrap_predicate_indicator(predicate)
    if arity < 0:
        error.throw_domain_error("not_less_than_zero", term.Number(arity))
    signature = name + "/" + str(arity)
    if signature in builtins:
        error.throw_permission_error("modify", "static_procedure",
                                     predicate)
    if signature in engine.signature2function:
        del engine.signature2function[signature]
Esempio n. 3
0
 def add_rule(self, rule, end=True):
     from pypy.lang.prolog import builtin
     if DEBUG:
         debug_print("add_rule", rule)
     if isinstance(rule, Term):
         if rule.name == ":-":
             rule = Rule(rule.args[0], rule.args[1])
         else:
             rule = Rule(rule, None)
         signature = rule.signature
     elif isinstance(rule, Atom):
         rule = Rule(rule, None)
         signature = rule.signature
     else:
         error.throw_type_error("callable", rule)
         assert 0, "unreachable" # make annotator happy
     if signature in builtin.builtins:
         error.throw_permission_error(
             "modify", "static_procedure", rule.head.get_prolog_signature())
     function = self._lookup(signature)
     function.add_rule(rule, end)
Esempio n. 4
0
def impl_retract(engine, pattern):
    from pypy.lang.prolog.builtin import builtins
    if isinstance(pattern, term.Term) and pattern.name == ":-":
        head = helper.ensure_callable(pattern.args[0])
        body = helper.ensure_callable(pattern.args[1])
    else:
        head = pattern
        body = None
    if head.signature in builtins:
        assert isinstance(head, term.Callable)
        error.throw_permission_error("modify", "static_procedure",
                                     head.get_prolog_signature())
    function = engine.signature2function.get(head.signature, None)
    if function is None:
        raise error.UnificationFailed
    #import pdb; pdb.set_trace()
    rulechain = function.rulechain
    while rulechain:
        rule = rulechain.rule
        oldstate = engine.heap.branch()
        # standardizing apart
        try:
            deleted_body = rule.clone_and_unify_head(engine.heap, head)
            if body is not None:
                body.unify(deleted_body, engine.heap)
        except error.UnificationFailed:
            engine.heap.revert(oldstate)
        else:
            if function.rulechain is rulechain:
                if rulechain.next is None:
                    del engine.signature2function[head.signature]
                else:
                    function.rulechain = rulechain.next
            else:
                function.remove(rulechain)
            break
        rulechain = rulechain.next
    else:
        raise error.UnificationFailed()
Esempio n. 5
0
def impl_retract(engine, pattern):
    from pypy.lang.prolog.builtin import builtins
    if isinstance(pattern, term.Term) and pattern.name == ":-":
        head = helper.ensure_callable(pattern.args[0])
        body = helper.ensure_callable(pattern.args[1])
    else:
        head = pattern
        body = None
    if head.signature in builtins:
        assert isinstance(head, term.Callable)
        error.throw_permission_error("modify", "static_procedure", 
                                     head.get_prolog_signature())
    function = engine.signature2function.get(head.signature, None)
    if function is None:
        raise error.UnificationFailed
    #import pdb; pdb.set_trace()
    rulechain = function.rulechain
    while rulechain:
        rule = rulechain.rule
        oldstate = engine.heap.branch()
        # standardizing apart
        try:
            deleted_body = rule.clone_and_unify_head(engine.heap, head)
            if body is not None:
                body.unify(deleted_body, engine.heap)
        except error.UnificationFailed:
            engine.heap.revert(oldstate)
        else:
            if function.rulechain is rulechain:
                if rulechain.next is None:
                    del engine.signature2function[head.signature]
                else:
                    function.rulechain = rulechain.next
            else:
                function.remove(rulechain)
            break
        rulechain = rulechain.next
    else:
        raise error.UnificationFailed()
Esempio n. 6
0
 def add_rule(self, rule, end=True):
     from pypy.lang.prolog import builtin
     if DEBUG:
         debug_print("add_rule", rule)
     if isinstance(rule, Term):
         if rule.name == ":-":
             rule = Rule(rule.args[0], rule.args[1])
         else:
             rule = Rule(rule, None)
         signature = rule.signature
     elif isinstance(rule, Atom):
         rule = Rule(rule, None)
         signature = rule.signature
     else:
         error.throw_type_error("callable", rule)
         assert 0, "unreachable"  # make annotator happy
     if signature in builtin.builtins:
         error.throw_permission_error("modify", "static_procedure",
                                      rule.head.get_prolog_signature())
     function = self.signature2function.get(signature, None)
     if function is not None:
         self.signature2function[signature].add_rule(rule, end)
     else:
         self.signature2function[signature] = Function(rule)