def test_if_(self): """Test if_.""" def return_nine(): return 9 def return_ten(): return 10 self.assertEqual(3, utils.if_(True, 3, 4)) self.assertEqual(4, utils.if_(False, 3, 4)) self.assertEqual(9, utils.if_(True, return_nine, return_ten)) self.assertEqual(10, utils.if_(False, return_nine, return_ten))
def unify(x, y, s): """Unify expressions x,y with substitution s; return a substitution that would make x,y equal, or None if x,y can not unify. x and y can be variables (e.g. Expr('x')), constants, lists, or Exprs. [Fig. 9.1] >>> unify(x + y, y + C, {}) {y: C, x: y} """ if s is None: return None elif x == y: return s elif is_variable(x): return unify_var(x, y, s) elif is_variable(y): return unify_var(y, x, s) elif isinstance(x, Expr) and isinstance(y, Expr): return unify(x.args, y.args, unify(x.op, y.op, s)) elif isterm(x) or isterm(y) or not x or not y: return utils.if_(x == y, s, None) elif utils.is_sequence(x) and utils.is_sequence(y) and len(x) == len(y): return unify(x[1:], y[1:], unify(x[0], y[0], s)) else: return None