|
| 1 | +# UNIFY |
| 2 | + |
| 3 | +## AIMA3e |
| 4 | +__function__ UNIFY(_x_, _y_, _θ_) __returns__ a substitution to make _x_ and _y_ identical |
| 5 | + __inputs__: _x_, a variable, constant, list, or compound |
| 6 | +    _y_, a variable, constant, list, or compound |
| 7 | +    _θ_, the substitution built up so far (optional, defaults to empty) |
| 8 | + |
| 9 | + __if__ _θ_ = failure __then return__ failure |
| 10 | + __else if__ _x_ = _y_ __then return__ _θ_ |
| 11 | + __else if__ VARIABLE?(_x_) __then return__ UNIVY-VAR(_x_, _y_, _θ_) |
| 12 | + __else if__ VARIABLE?(_y_) __then return__ UNIFY-VAR(_y_, _x_, _θ_) |
| 13 | + __else if__ COMPOUND?(_x_) and COMPOUND?(_y_) __then__ |
| 14 | +   __return__ UNIFY(_x_.ARGS, _y_.ARGS, UNIFY(_x_.OP, _y_.OP, _θ_)) |
| 15 | + __else if__ LIST?(_x_) __and__ LIST?(_y_) __then__ |
| 16 | +   __return__ UNIFY(_x_.REST, _y_.REST, UNIFY(_x_.FIRST, _y_.FIRST, _θ_)) |
| 17 | + __else return__ failure |
| 18 | + |
| 19 | +--- |
| 20 | +__function__ UNIFY-VAR(_var_, _x_, _θ_) __returns__ a substitution |
| 21 | + |
| 22 | + __if__ {_var_ / _val_} ∈ _θ_ __then return__ UNIFY(_val_, _x_, _θ_) |
| 23 | + __else if__ {_x_ / _val_} ∈ _θ_ __then return__ UNIFY(_var_, _val_, _θ_) |
| 24 | + __else if__ OCCUR-CHECK?(_var_, _x_) __then return__ failure |
| 25 | + __else return__ add {_var_/_x_} to _θ_ |
| 26 | + |
| 27 | +--- |
| 28 | +__Figure__ ?? The unification algorithm. The algorithm works by comparing the structures of the inputs, elements by element. The substitution _θ_ that is the argument to UNIFY is built up along the way and is used to make sure that later comparisons are consistent with bindings that were established earlier. In a compound expression, such as F(A, B), the OP field picks out the function symbol F and the ARGS field picks out the argument list (A, B). |
0 commit comments