Beispiel #1
0
def impl_if(engine, if_clause, then_clause, continuation):
    oldstate = engine.heap.branch()
    try:
        engine.call(if_clause)
    except error.UnificationFailed:
        engine.heap.revert(oldstate)
        raise
    return engine.call(helper.ensure_callable(then_clause), continuation,
                       choice_point=False)
Beispiel #2
0
def impl_if(engine, if_clause, then_clause, continuation):
    oldstate = engine.heap.branch()
    try:
        engine.call(if_clause)
    except error.UnificationFailed:
        engine.heap.revert(oldstate)
        raise
    return engine.call(helper.ensure_callable(then_clause),
                       continuation,
                       choice_point=False)
Beispiel #3
0
 def __init__(self, head, body):
     from pypy.lang.prolog.interpreter import helper
     assert isinstance(head, Callable)
     self.head = head
     if body is not None:
         body = helper.ensure_callable(body)
         self.body = body
     else:
         self.body = None
     self.signature = self.head.signature
     if isinstance(head, Term):
         self.unify_hash = [arg.get_unify_hash(None) for arg in head.args]
     self._does_contain_cut()
Beispiel #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()
Beispiel #5
0
 def __init__(self, head, body):
     from pypy.lang.prolog.interpreter import helper
     assert isinstance(head, Callable)
     self.head = head
     if body is not None:
         body = helper.ensure_callable(body)
         self.body = body
     else:
         self.body = None
     self.signature = self.head.signature
     if isinstance(head, Term):
         self.unify_hash = [arg.get_unify_hash(None) for arg in head.args]
     self._does_contain_cut()
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()
Beispiel #7
0
 def _call(self, engine):
     next_call = self.next_call.dereference(engine.heap)
     next_call = helper.ensure_callable(next_call)
     return engine.call(next_call, self.continuation, choice_point=False)
Beispiel #8
0
 def _call(self, engine):
     next_call = self.next_call.dereference(engine.heap)
     next_call = helper.ensure_callable(next_call)
     return engine.call(next_call, self.continuation, choice_point=False)