def applyo_goal(S): nonlocal o_rator, o_rands, obj o_rator_rf, o_rands_rf, obj_rf = reify((o_rator, o_rands, obj), S) if not isvar(obj_rf): # We should be able to use this goal with *any* arguments, so # fail when the ground operations fail/err. try: obj_rator, obj_rands = operator(obj_rf), arguments(obj_rf) except (ConsError, NotImplementedError): return # The object's rator + rands should be the same as the goal's yield from lall(eq(o_rator_rf, obj_rator), eq(o_rands_rf, obj_rands))(S) elif isvar(o_rands_rf) or isvar(o_rator_rf): # The object and at least one of the rand, rators is a logic # variable, so let's just assert a `cons` relationship between # them yield from conso(o_rator_rf, o_rands_rf, obj_rf)(S) else: # The object is a logic variable, but the rator and rands aren't. # We assert that the object is the application of the rand and # rators. try: obj_applied = term(o_rator_rf, o_rands_rf) except (ConsError, NotImplementedError): return yield from eq(obj_rf, obj_applied)(S)
def test_isvar(): assert not isvar(3) assert isvar(var(3)) class CustomVar(Var): pass assert isvar(CustomVar())
def _unify_ConstrainedVar_object(u, v, s): u_w = walk(u, s) if isvar(v): v_w = walk(v, s) else: v_w = v if u_w == v_w: yield s elif isvar(u_w): if (not isvar(v_w) and isinstance(u_w, ConstrainedVar) and not u_w.constraint(eval_if_etuple(v_w))): yield False return yield assoc(s, u_w, v_w) elif isvar(v_w): if (not isvar(u_w) and isinstance(v_w, ConstrainedVar) and not v_w.constraint(eval_if_etuple(u_w))): yield False return yield assoc(s, v_w, u_w) else: yield _unify(u_w, v_w, s)
def test_context_manager(): with variables(1): assert isvar(1) assert not isvar(1)
def test_isvar(): assert not isvar(3) assert isvar(var(3))