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)
def unify(self, goal): subs = self.conclusion.unify(goal) if subs is not None and len(subs) == 1: x = list(self.variables)[0] lhs, rhs = list(subs.items())[0] if rhs is x: lhs, rhs = rhs, lhs if isinstance(rhs, Variable): res = Substitution() res.replace(rhs, self.rules_db.introduce_constant()) return res return None
def refreshing_substitution(self, vars): refreshing_subs = Substitution() for v in vars: refreshing_subs.replace(v, self.introduce_variable(v)) return refreshing_subs