Commits

David Schneider committed 8d60bd5

add helper method is_term and use it

Comments (0)

Files changed (4)

prolog/builtin/control.py

 def impl_or(engine, heap, call1, call2, scont, fcont):
     # sucks a bit to have to special-case A -> B ; C here :-(
     if call1.signature()== "->/2":
-        assert isinstance(call1, term.Term)
+        assert helper.is_term(call1)
         scont = fcont = continuation.CutDelimiter(engine, scont, fcont)
         fcont = OrContinuation(engine, scont, heap, fcont, call2)
         newscont, fcont, heap = impl_if(

prolog/builtin/database.py

 @expose_builtin("retract", unwrap_spec=["callable"])
 def impl_retract(engine, heap, pattern):
     from prolog.builtin import builtins
-    if isinstance(pattern, term.Term) and pattern.name()== ":-":
+    if helper.is_term(pattern) and pattern.name()== ":-":
         head = helper.ensure_callable(pattern.argument_at(0))
         body = helper.ensure_callable(pattern.argument_at(1))
     else:

prolog/interpreter/continuation.py

 import py
 from pypy.rlib import jit
 from prolog.interpreter import error
+from prolog.interpreter import helper
 from prolog.interpreter.term import Term, Atom, Var, Callable
 from prolog.interpreter.function import Function, Rule
 
 
     def add_rule(self, rule, end=True):
         from prolog import builtin
-        if isinstance(rule, Term):
+        if helper.is_term(rule):
             if rule.name()== ":-":
                 rule = Rule(rule.argument_at(0), rule.argument_at(1))
             else:

prolog/interpreter/helper.py

     return (isinstance(obj, term.Atom) or isinstance(obj, term.Float) or 
             isinstance(obj, term.Number))
 
-
+def is_term(obj):
+    return (not isinstance(obj, term.Atom) and isinstance(obj, term.Callable)
+            and obj.argument_count() > 0)
 def convert_to_str(obj):
     if isinstance(obj, term.Var):
         error.throw_instantiation_error()