def deref(self, env): envValue = env[self] if not isinstance(envValue, Var): return envValue next = env.bindings.get(envValue, None) if next is None: return envValue if next is self: return next result = deref(next, env) if result is not envValue and not isinstance(envValue, RuleHeadCopyVar): self.setvalue(result, env) return result
def take_value(self, env): envValue = env[self] if not isinstance(envValue, Var): result = envValue else: next = env.bindings.get(envValue, None) if next is None: result = envValue elif next is self: result = next else: result = deref(next, env) if not isinstance(result, Var): return take_value(result, env) return result
def unify(self, other, env, occurs_check=False): self = self.deref(env) other = deref(other, env) if isinstance(self, Var): if isinstance(other, Var) and other is self: yield True return if occurs_check and contain_var(other, self):return self.setvalue(other, env) yield True try: del env.bindings[self] # for DummyVar except: pass elif isinstance(other, Var): if occurs_check and contain_var(self, other): return other.setvalue(self, env) yield True try: del env.bindings[other] # for DummyVar except: pass else: for result in unify(self, other, env, occurs_check): yield True