Intelligence 2E

foundations of computational agents

The proof procedures with variables carry over for the case with function symbols. The main difference is that the class of terms is expanded to include function symbols.

The use of function symbols involves infinitely many terms. This means that, when forward chaining on the clauses, we have to ensure that the selection criterion for selecting clauses is fair.

To see why fairness is important, consider the following clauses:

${n}{}{u}{}{m}{}{(}{0}{)}{.}$ | ||

${n}{}{u}{}{m}{}{(}{s}{}{(}{N}{)}{)}{\leftarrow}{}{n}{}{u}{}{m}{}{(}{N}{)}{.}$ | ||

${a}{\leftarrow}{}{b}{.}$ | ||

${b}{.}$ |

An unfair strategy could initially select the first of these clauses to forward chain on and, for every subsequent selection, select the second clause. The second clause can always be used to derive a new consequence. This strategy never selects either of the last two clauses and thus never derives ${a}$ or ${b}$.

This problem of ignoring some clauses forever is known as starvation. A fair selection criterion is one such that any clause available to be selected will eventually be selected. The bottom-up proof procedure can generate an infinite sequence of consequences and if the selection is fair, each consequence will eventually be generated and so the proof procedure is complete.

The top-down proof procedure is the same as for Datalog (see Figure 13.4). Unification becomes more complicated, because it must recursively descend into the structure of terms. There is one change to the unification algorithm: a variable $X$ does not unify with a term $t$ in which $X$ occurs and is not $X$ itself. Checking for this condition is known as the occurs check. If the occurs check is not used and a variable is allowed to unify with a term in which it appears, the proof procedure becomes unsound, as shown in the following example.

Consider the knowledge base with only one clause:

$${l}{}{t}{}{(}{X}{,}{s}{}{(}{X}{)}{)}{.}$$ |

Suppose the intended interpretation is the domain of integers in which ${l}{\mathit{}}{t}$ means “less than” and ${s}{\mathit{}}{\mathrm{(}}{X}{\mathrm{)}}$ denotes the integer after ${X}$. The query ${\text{\U0001d5ba\U0001d5cc\U0001d5c4}}{\mathit{\text{}}}{\mathit{}}{l}{\mathit{}}{t}{\mathit{}}{\mathrm{(}}{Y}{\mathrm{,}}{Y}{\mathrm{)}}$ should fail because it is false in the intended interpretation; there is no number less than itself. However, if ${X}$ and ${s}{\mathit{}}{\mathrm{(}}{X}{\mathrm{)}}$ could unify, this query would succeed. In this case, the proof procedure would be unsound because something could be derived that is false in a model of the axioms.

The unification algorithm of Figure 13.3 requires one change to find the most general unifier of two terms with function symbols. The algorithm should return $\perp $ if it selects an equality $\alpha =\beta $, where $\alpha $ is a variable and $\beta $ is a term that is not $\alpha $, but contains $\alpha $ (or the other way around). This step is the occurs check. The occurs check is sometimes omitted (e.g., in Prolog), because removing it makes the proof procedure more efficient, even though removing it does make the proof procedure unsound.

The following example shows the details of SLD resolution with function symbols.

Consider the clauses

${a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{c}{}{(}{A}{,}{X}{)}{,}{Y}{,}{c}{}{(}{A}{,}{Z}{)}{)}{\leftarrow}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{X}{,}{Y}{,}{Z}{)}{.}$ | ||

${a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{n}{}{i}{}{l}{,}{Z}{,}{Z}{)}{.}$ |

For now, ignore what this may mean. Like the computer, treat this as a problem of symbol manipulation. Consider the following query:

$${\text{\U0001d622\U0001d634\U0001d62c}}{\mathit{\text{}}}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{F}{,}{c}{}{(}{L}{,}{n}{}{i}{}{l}{)}{,}{c}{}{(}{l}{,}{c}{}{(}{i}{,}{c}{}{(}{s}{,}{c}{}{(}{t}{,}{n}{}{i}{}{l}{)}{)}{)}{)}{)}{.}$$ |

The following is a derivation:

${y}{}{e}{}{s}{}{(}{F}{,}{L}{)}{\leftarrow}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{F}{,}{c}{}{(}{L}{,}{n}{}{i}{}{l}{)}{,}{c}{}{(}{l}{,}{c}{}{(}{i}{,}{c}{}{(}{s}{,}{c}{}{(}{t}{,}{n}{}{i}{}{l}{)}{)}{)}{)}{)}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{resolve with}}}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{c}{}{(}{{A}}_{{1}}{,}{{X}}_{{1}}{)}{,}{{Y}}_{{1}}{,}{c}{}{(}{{A}}_{{1}}{,}{{Z}}_{{1}}{)}{)}{\leftarrow}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{{X}}_{{1}}{,}{{Y}}_{{1}}{,}{{Z}}_{{1}}{)}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{substitution:}}}{}{\{}{F}{/}{c}{}{(}{l}{,}{{X}}_{{1}}{)}{,}{{Y}}_{{1}}{/}{c}{}{(}{L}{,}{n}{}{i}{}{l}{)}{,}{{A}}_{{1}}{/}{l}{,}{{Z}}_{{1}}{/}{c}{}{(}{i}{,}{c}{}{(}{s}{,}{c}{}{(}{t}{,}{n}{}{i}{}{l}{)}{)}{)}{\}}$ | ||

${y}{}{e}{}{s}{}{(}{c}{}{(}{l}{,}{{X}}_{{1}}{)}{,}{L}{)}{\leftarrow}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{{X}}_{{1}}{,}{c}{}{(}{L}{,}{n}{}{i}{}{l}{)}{,}{c}{}{(}{i}{,}{c}{}{(}{s}{,}{c}{}{(}{t}{,}{n}{}{i}{}{l}{)}{)}{)}{)}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{resolve with}}}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{c}{}{(}{{A}}_{{2}}{,}{{X}}_{{2}}{)}{,}{{Y}}_{{2}}{,}{c}{}{(}{{A}}_{{2}}{,}{{Z}}_{{2}}{)}{)}{\leftarrow}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{{X}}_{{2}}{,}{{Y}}_{{2}}{,}{{Z}}_{{2}}{)}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{substitution:}}}{}{\{}{{X}}_{{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{}{(}{{X}}_{{3}}{,}{c}{}{(}{L}{,}{n}{}{i}{}{l}{)}{,}{c}{}{(}{t}{,}{n}{}{i}{}{l}{)}{)}$ |

At this stage both clauses are applicable. Choosing the first clause gives

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{resolve with}}}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{c}{}{(}{{A}}_{{4}}{,}{{X}}_{{4}}{)}{,}{{Y}}_{{4}}{,}{c}{}{(}{{A}}_{{4}}{,}{{Z}}_{{4}}{)}{)}{\leftarrow}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{{X}}_{{4}}{,}{{Y}}_{{4}}{,}{{Z}}_{{4}}{)}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{substitution:}}}{}{\{}{{X}}_{{3}}{/}{c}{}{(}{t}{,}{{X}}_{{4}}{)}{,}{{Y}}_{{4}}{/}{c}{}{(}{L}{,}{n}{}{i}{}{l}{)}{,}{{A}}_{{4}}{/}{t}{,}{{Z}}_{{4}}{/}{n}{}{i}{}{l}{\}}$ | ||

${y}{}{e}{}{s}{}{(}{c}{}{(}{l}{,}{c}{}{(}{i}{,}{c}{}{(}{s}{,}{{X}}_{{3}}{)}{)}{)}{,}{L}{)}{\leftarrow}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{{X}}_{{4}}{,}{c}{}{(}{L}{,}{n}{}{i}{}{l}{)}{,}{n}{}{i}{}{l}{)}$ |

At this point, there are no clauses whose head unifies with the atom in the generalized answer clause’s body. The proof fails.

Choosing the second clause instead of the first gives

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{resolve with}}}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{n}{}{i}{}{l}{,}{{Z}}_{{5}}{,}{{Z}}_{{5}}{)}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{\mathit{\text{substitution:}}}{}{\{}{{Z}}_{{5}}{/}{c}{}{(}{t}{,}{n}{}{i}{}{l}{)}{,}{{X}}_{{3}}{/}{n}{}{i}{}{l}{,}{L}{/}{t}{\}}$ | ||

${y}{}{e}{}{s}{}{(}{c}{}{(}{l}{,}{c}{}{(}{i}{,}{c}{}{(}{s}{,}{n}{}{i}{}{l}{)}{)}{)}{,}{t}{)}{\leftarrow}$ |

At this point, the proof succeeds, with answer ${F}{\mathrm{=}}{c}{\mathit{}}{\mathrm{(}}{l}{\mathrm{,}}{c}{\mathit{}}{\mathrm{(}}{i}{\mathrm{,}}{c}{\mathit{}}{\mathrm{(}}{s}{\mathrm{,}}{n}{\mathit{}}{i}{\mathit{}}{l}{\mathrm{)}}{\mathrm{)}}{\mathrm{)}}$, ${L}{\mathrm{=}}{t}$.

For the rest of this chapter, we use the “syntactic sugar” notation of Prolog for representing lists. The empty list, $nil$, is written as $[]$. The list with first element $E$ and the rest of the list $R$, which was $cons(E,R)$ in Example 13.29 is now written as $[E\mid R]$. There is one other notational simplification: $[X\mid [Y]]$ is written as $[X,Y]$, where $Y$ can be a sequence of values. For example, $[a\mid []]$ is written as $[a]$, and $[b\mid [a\mid []]]$ is written as $[b,a]$. The term $[a\mid [b\mid C]]$ is written as $[a,b\mid C]$.

Using the list notation, ${a}{\mathit{}}{p}{\mathit{}}{p}{\mathit{}}{e}{\mathit{}}{n}{\mathit{}}{d}$ from the previous example can be written as

${a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{[}{A}{\mid}{X}{]}{,}{Y}{,}{[}{A}{\mid}{Z}{]}{)}{\leftarrow}$ | ||

${\mathrm{}}{\mathit{\hspace{1em}\hspace{1em}\u2006}}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{X}{,}{Y}{,}{Z}{)}{.}$ | ||

${a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{[}{]}{,}{Z}{,}{Z}{)}{.}$ |

The query

$${\text{\U0001d622\U0001d634\U0001d62c}}{\mathit{\text{}}}{}{a}{}{p}{}{p}{}{e}{}{n}{}{d}{}{(}{F}{,}{[}{L}{]}{,}{[}{l}{,}{i}{,}{s}{,}{t}{]}{)}$$ |

has an answer ${F}{\mathrm{=}}{\mathrm{[}}{l}{\mathrm{,}}{i}{\mathrm{,}}{s}{\mathrm{]}}$, ${L}{\mathrm{=}}{t}$. The proof is exactly as in the previous example. As far as the proof procedure is concerned, nothing has changed; there is just a renamed function symbol and constant.