def unify(self, expr): subs = Substitution() eqs = [(self, expr)] while eqs: lhs, rhs = eqs.pop() if lhs is rhs: continue elif isinstance(lhs, Application) and isinstance(rhs, Application): eqs.extend(zip(lhs.elems, rhs.elems)) elif isinstance(lhs, Variable) or isinstance(rhs, Variable): if not isinstance(lhs, Variable): lhs, rhs = rhs, lhs if isinstance(rhs, Variable) or not rhs.contains_leaf(lhs): add_subs = Substitution() add_subs.replace(lhs, rhs) subs.compose(add_subs) eqs = [(add_subs.apply(lhs), add_subs.apply(rhs)) for lhs, rhs in eqs] else: return None else: return None return subs
def instantiate_free_variables(self, expr): vars = expr.collect_variables() instantiate_vars_subs = Substitution() for v in vars: instantiate_vars_subs.replace(v, self.introduce_constant()) return instantiate_vars_subs.apply(expr)