UNIFICATION THEOREM Given the question, do 2 formulas match? There is a unique answer, and an algorithm to find it. Just like for sorting, there are slower (O(n**2)) and faster (O(n)) algorithms. The slow ones are good enough for dialogue system use. Convention: When 2 formulas don't match, the algorithm returns nil = (). When 2 formulas match but no variables need to be set, the algorithm returns (nil) = (()). Example: match (is-red raspberry) against the DB. If (is-red raspberry) is in the DB, return (nil). If it's not, return nil. ---------------------------------------------------------- RECURSIVE UNIFICATION ALGORITHM Input: 2 formulas Output: one of three possibilities: - A binding list, i.e. details of how formulas match up - (nil), i.e. the algorithm has succeeded but no bindings are necessary - nil, i.e. failure 1. If the formulas are equal, then return, i.e. success 2. If either formula is a variable, add the following item to the binding list: x <-- other-formula (If both formulas are simple variables, it doesn't matter whether you choose x <-- y or y <-- x.) (If x occurs in other-formula, then we have an infinite loop, so this case should fail. This won't happen in applications like ours except for typos/bugs.) Then return, i.e. success. 3. If either formula is a constant, then fail (we already know that they aren't equal). ;; Now both formulas must be relational expressions 4. If the two formulas are different lengths, then fail (they can't be equal). ;; Now both formulas must be relational expressions with the same number of arguments 5. Call the unification algorithm for each argument. If all succeed, return, i.e. success. If any pair fails, then fail. ---------------------------------------------------------- ITERATIVE UNIFICATION ALGORITHM Input: 2 formulas Output: One of three possibilities: - A binding list, i.e. details of how formulas match up - (nil), i.e. the algorithm has succeeded but no bindings are necessary - nil, i.e. failure Steps: 1. Find the first disagreement point of the two formulas, i.e. the leftmost character which is not identical. If there is none if the binding list is empty, return (nil), i.e. success with no bindings else return the binding list, i.e. success 2. Try to extract a valid binding from the disagreement. A valid binding means that one of the formulas has a variable and the other one has a (sub)formula not containing the variable that starts at the same point. If there is one add it to the binding list and apply it to the formulas else return nil, i.e. failure. 3. Loop back to step 1.