public override Term ApplySubstitution(Substitution sigma) { // Does sigma apply to our variable? Term replacement = sigma.GetMapping(this); if (replacement == null) return this; // nothing to change! // Otherwise, return the variable's replacement return replacement; }
public override bool Mgu(Term t, Substitution subsSoFar) { if (t is TermObject) { Term replacement = subsSoFar.GetMapping(this); if (replacement != null) { TermVariable termVariable = replacement as TermVariable; if (termVariable != null) { subsSoFar.AddMapping(this, t); subsSoFar.AddMapping(termVariable, t); return true; } return replacement.Equals(t); } // There was no replacement: // Add a mapping for the variable to this term-object subsSoFar.AddMapping(this, t); return true; } var variable = t as TermVariable; if (variable != null) { TermVariable it = variable; Term myReplacement = subsSoFar.GetMapping(this); Term itsReplacement = subsSoFar.GetMapping(it); if (itsReplacement == null) { // just map 'it' to me (or my replacement) if (myReplacement == null) { if (!Equals(it)) subsSoFar.AddMapping(it, this); } else { if (!(myReplacement is TermVariable) || !myReplacement.Equals(it)) subsSoFar.AddMapping(it, myReplacement); } return true; } // At this point, 'it' has a replacement. if (myReplacement == null) { // I don't have a replacement, so map me to it, or to its replacement if (!(itsReplacement is TermVariable) || !itsReplacement.Equals(this)) subsSoFar.AddMapping(this, itsReplacement); return true; } // At this point, both term variables have replacements. // So make sure that they are the same! return myReplacement.Equals(itsReplacement); } var func = t as TermFunction; if (func != null) { Term myReplacement = subsSoFar.GetMapping(this); // Case 1: I have a replacement if (myReplacement != null) // See if my replacement can be unified with the function return myReplacement.Mgu(func, subsSoFar); // Case 2: I have no replacement TermFunction itsReplacement = subsSoFar.GetMapping(func); if (itsReplacement.HasVariable(this)) return false; // just set my replacement to the function subsSoFar.AddMapping(this, itsReplacement); return true; } throw new Exception("TermVariable.mgu: Don't know how to handle term of type " + t.GetType().Name); }
protected bool Equals(Substitution other) { if (other.NumMappings() != NumMappings()) return false; List<TermVariable> subs = Substitutions.Keys.ToList(); //this cannot be a foreach as GetMapping possibly modifies the collection for (int i = 0; i < subs.Count; i++) { TermVariable currrentKey = subs[i]; if (!GetMapping(currrentKey).Equals(other.GetMapping(currrentKey))) return false; } return true; //return other.NumMappings() == NumMappings() && _substitutions.All(kv => GetMapping(kv.Key).Equals(other.GetMapping(kv.Key))); }