foundations of computational agents
Both the bottom-up and top-down propositional proof procedures of Section 5.3.2 can be extended to Datalog. A proof procedure extended for variables must account for the fact that a free variable in a clause means that all instances of the clause are true. A proof may have to use different instances of the same clause in a single proof.
An instance of a clause is obtained by uniformly substituting terms for variables in the clause. All occurrences of a particular variable are replaced by the same term.
The specification of which value is assigned to each variable is called a substitution. A substitution is a set of the form $\{{V}_{1}/{t}_{1},\mathrm{\dots},{V}_{n}/{t}_{n}\}$, where each ${V}_{i}$ is a distinct variable and each ${t}_{i}$ is a term. The element ${V}_{i}/{t}_{i}$ is a binding for variable ${V}_{i}$. A substitution is in normal form if no ${V}_{i}$ appears in any ${t}_{j}$.
For example, $\{X/Y,Z/chile\}$ is a substitution in normal form that binds $X$ to $Y$ and binds $Z$ to $chile$. The substitution$\{X/Y,Z/X\}$ is not in normal form, because the variable $X$ occurs both on the left and on the right of a binding.
The application of a substitution $\sigma =\{{V}_{1}/{t}_{1},\mathrm{\dots},{V}_{n}/{t}_{n}\}$ to expression $e$, written $e\sigma $, is an expression that is the same as the original expression $e$ except that each occurrence of ${V}_{i}$ in $e$ is replaced by the corresponding ${t}_{i}$. The expression $e\sigma $ is called an instance of $e$. If $e\sigma $ does not contain any variables, it is called a ground instance of $e$.
Some applications of substitutions are
$borders(peru,X)\{X/chile\}=borders(peru,chile).$ |
$borders(Y,chile)\{Y/peru\}=borders(peru,chile).$ |
$borders(peru,X)\{Y/peru,Z/X\}=borders(peru,X).$ |
$p(X,X,Y,Y,Z)\{X/Z,Y/brazil\}=p(Z,Z,brazil,brazil,Z).$ |
Substitutions can apply to clauses, atoms, and terms. For example, the result of applying the substitution $\{X/Y,Z/peru\}$ to the clause
$$p(X,Y)\leftarrow q(peru,Z,X,Y,Z)$$ |
is the clause
$$p(Y,Y)\leftarrow q(peru,peru,Y,Y,peru).$$ |
A substitution $\sigma $ is a unifier of expressions ${e}_{1}$ and ${e}_{2}$ if ${e}_{1}\sigma $ is identical to ${e}_{2}\sigma $; expressions ${e}_{1}$ and ${e}_{2}$ are said to unify. That is, a unifier of two expressions is a substitution that when applied to each expression results in the same expression.
Substitution $\{X/chile,Y/peru\}$ is a unifier of $borders(peru,X)$ and $borders(Y,chile)$. Applying this substitution to either gives $borders(peru,chile)$.
$\{X/a,Y/b\}$ is a unifier of $t(a,Y,c)$ and $t(X,b,c)$ as
$$t(a,Y,c)\{X/a,Y/b\}=t(X,b,c)\{X/a,Y/b\}=t(a,b,c).$$ |
Expressions can have many unifiers.
Atoms $p(X,Y)$ and $p(Z,Z)$ have many unifiers, including $\{X/b,Y/b,Z/b\}$, $\{X/c,Y/c,Z/c\}$, $\{X/Z,Y/Z\}$, and $\{Y/X,Z/X\}$. The last two unifiers are more general than the first two, because the first two both have $X$ the same as $Z$ and $Y$ the same as $Z$ but make more commitments to what these values are.
Substitution $\sigma $ is a most general unifier (MGU) of expressions ${e}_{1}$ and ${e}_{2}$ if
$\sigma $ is a unifier of the two expressions, and
if substitution ${\sigma}^{\prime}$ is also a unifier of ${e}_{1}$ and ${e}_{2}$, then $e{\sigma}^{\prime}$ must be an instance of $e\sigma $ for all expressions $e$.
$\{X/Z,Y/Z\}$ and $\{Z/X,Y/X\}$ are both most general unifiers of $p(X,Y)$ and $p(Z,Z)$. $\{X/a,Y/a,Z/a\}$ is a unifier, but not a most general unifier, of these. The resulting applications are
$p(X,Y)\{X/Z,Y/Z\}=p(Z,Z)\{X/Z,Y/Z\}=p(Z,Z)$ |
$p(X,Y)\{Z/X,Y/X\}=p(Z,Z)\{Z/X,Y/X\}=p(X,X)$ |
$p(X,Y)\{X/a,Y/a,Z/a\}=p(Z,Z)\{X/a,Y/a,Z/a\}=p(a,a).$ |
Note that $p(a,a)$ is an instance of $p(Z,Z)$ and an instance of $p(X,X)$, but these are not instances of $p(a,a)$. $p(Z,Z)$ and $p(X,X)$ are instances of each other.
The definition of MGU refers to “all expressions $e$” to preclude a substitution such as $\{X/Z,Y/Z,W/a\}$ being a most general unifier of $p(X,Y)$ and $p(Z,Z)$, because it affects other expressions such as $r(W)$.
Expression ${e}_{1}$ is a renaming of ${e}_{2}$ if they differ only in the names of variables. In this case, they are both instances of each other.
If two expressions have a unifier, they have at least one MGU. The expressions resulting from applying the MGUs to the expressions are all renamings of each other. That is, if $\sigma $ and ${\sigma}^{\prime}$ are both MGUs of expressions ${e}_{1}$ and ${e}_{2}$, then ${e}_{1}\sigma $ is a renaming of ${e}_{1}{\sigma}^{\prime}$.
The propositional bottom-up proof procedure can be extended to Datalog by using ground instances of the clauses. A ground instance of a clause is obtained by uniformly substituting constants for the variables in the clause. The constants required are those appearing in the knowledge base or in the query. If there are no constants in the knowledge base or the query, one must be invented.
Suppose the knowledge base is
$$\begin{array}{ccc}q(a).\hfill & q(b).\hfill & r(a).\hfill \\ s(W)\leftarrow r(W).\hfill & p(X,Y)\leftarrow q(X)\wedge s(Y).\hfill & \end{array}$$ |
The set of all ground instances is
$$\begin{array}{ccc}q(a).\hfill & q(b).\hfill & r(a).\hfill \\ s(a)\leftarrow r(a).\hfill & s(b)\leftarrow r(b).\hfill & p(a,a)\leftarrow q(a)\wedge s(a).\hfill \\ p(a,b)\leftarrow q(a)\wedge s(b).\hfill & p(b,a)\leftarrow q(b)\wedge s(a).\hfill & p(b,b)\leftarrow q(b)\wedge s(b).\hfill \end{array}$$ |
The propositional bottom-up proof procedure of Section 5.3.2 can be applied to the grounding to derive $q(a)$, $q(b)$, $r(a)$, $s(a)$, $p(a,a)$, and $p(b,a)$ as the ground instances that are logical consequences.
Suppose the knowledge base is
$p(X,Y).$ |
$g\leftarrow p(W,W).$ |
The bottom-up proof procedure for query “$\text{ask}\text{}g$” must invent a new constant symbol, say $c$. The set of all ground instances is then
$p(c,c).$ |
$g\leftarrow p(c,c).$ |
The propositional bottom-up proof procedure will derive $p(c,c)$ and $g$.
If the query were “$\text{ask}\text{}p(b,d)$” the set of ground instances would change to reflect the constants $b$ and $d$.
The bottom-up proof procedure applied to the grounding of the knowledge base is sound, because each instance of each rule is true in every model. This procedure is essentially the same as the variable-free case, but it uses the set of ground instances of the clauses, all of which are true because the variables in a clause are universally quantified.
This bottom-up procedure will eventually halt for Datalog because there are only finitely many grounded atoms, and one ground atom is added to the consequence set each time through the loop.
This procedure is also complete for ground atoms. That is, if a ground atom is a consequence of the knowledge base, it will be derived. To prove this, as in the propositional case, construct a particular generic model. Recall that a model specifies the domain, what the constants denote, and what is true. A Herbrand interpretation is an interpretation where the domain is symbolic and consists of all constants of the language. A constant is invented if there are no constants in the knowledge base or the query. In a Herbrand interpretation, each constant denotes itself. Thus, in the definition of an interpretation, $D$ and $\varphi $ are fixed for a given program, and all that needs to be specified is $\pi $, which defines the predicate symbols.
Consider the Herbrand interpretation where the true atoms are the ground instances of the relations that are eventually derived by the bottom-up procedure. It is easy to see that this Herbrand interpretation is a model of the rules given. As in the variable-free case, it is a minimal model in that it has the fewest true atoms of any model. If $KB\vDash g$ for ground atom $g$, then $g$ is true in the minimal model and, thus, is eventually derived.
A computer does not need to reason at the propositional level by grounding. Instead, it can reason in terms of variables instead of multiple ground instances. Sometimes, it needs to replace variables by other variables or by constants.
The problem of unification is the following: given two atoms or terms, determine whether they unify, and, if they do, return a unifier of them. The unification algorithm finds a most general unifier (MGU) of two atoms or returns $\perp $ if they do not unify.
The unification algorithm is given in Figure 15.4. $E$ is a set of equality statements implying and implied by the unification of ${t}_{1}$ and ${t}_{2}$. $S$ is a substitution in normal form; if $\alpha /\beta $ is in the substitution $S$, then, by construction, $\alpha $ is a variable that does not appear elsewhere in $S$ or in $E$. In line 20, $\alpha $ and $\beta $ must have the same predicate and the same number of arguments; otherwise the unification fails.
Consider the call $Unify(p(X,Y,Y),p(a,Z,b))$. Initially $E$ is $\{p(X,Y,Y)=p(a,Z,b)\}$. The first time through the while loop, $E$ becomes $\{X=a,Y=Z,Y=b\}$. Suppose $X=a$ is selected next. Then $S$ becomes $\{X/a\}$ and $E$ becomes $\{Y=Z,Y=b\}$. Suppose $Y=Z$ is selected. Then $Y$ is replaced by $Z$ in $S$ and $E$. $S$ becomes $\{X/a,Y/Z\}$ and $E$ becomes $\{Z=b\}$. Finally $Z=b$ is selected, $Z$ is replaced by $b$, $S$ becomes $\{X/a,Y/b,Z/b\}$, and $E$ becomes empty. The substitution $\{X/a,Y/b,Z/b\}$ is returned as an MGU.
Consider unifying $p(a,Y,Y)$ with $p(Z,Z,b)$. $E$ starts off as $\{p(a,Y,Y)=p(Z,Z,b)\}$. In the next step, $E$ becomes $\{a=Z,Y=Z,Y=b\}$. Then $Z$ is replaced by $a$ in $E$, and $E$ becomes $\{Y=a,Y=b\}$. Then $Y$ is replaced by $a$ in $E$, and $E$ becomes $\{a=b\}$, and then $\perp $ is returned indicating that there is no unifier.
The top-down proof procedure for propositional definite clauses can be extended to handle variables by allowing instances of rules to be used in the derivation.
A generalized answer clause is of the form
$yes({t}_{1},\mathrm{\dots},{t}_{k})\leftarrow {a}_{1}\wedge {a}_{2}\wedge \mathrm{\dots}\wedge {a}_{m}$ |
where ${t}_{1},\mathrm{\dots},{t}_{k}$ are terms and ${a}_{1},\mathrm{\dots},{a}_{m}$ are atoms. The use of $yes$ enables answer extraction: determining which instances of the query variables are a logical consequence of the knowledge base.
Initially, the generalized answer clause for query $q$ is
$$yes({V}_{1},\mathrm{\dots},{V}_{k})\leftarrow q$$ |
where ${V}_{1},\mathrm{\dots},{V}_{k}$ are the variables that appear in the query $q$. Intuitively this means that an instance of $yes({V}_{1},\mathrm{\dots},{V}_{k})$ is true if the corresponding instance of the query is true.
The proof procedure maintains a current generalized answer clause.
At each stage, the algorithm selects an atom in the body of the generalized answer clause. It then chooses a clause in the knowledge base whose head unifies with the atom.
The SLD resolution where SLD stands for selection linear definite, of the generalized answer clause
$$yes({t}_{1},\mathrm{\dots},{t}_{k})\leftarrow {a}_{1}\wedge {a}_{2}\wedge \mathrm{\dots}\wedge {a}_{m}$$ |
on ${a}_{1}$ with the chosen clause
$a\leftarrow {b}_{1}\wedge \mathrm{\dots}\wedge {b}_{p}$ |
where ${a}_{1}$ and $a$ have most general unifier $\sigma $, is the answer clause
$(yes({t}_{1},\mathrm{\dots},{t}_{k})\leftarrow {b}_{1}\wedge \mathrm{\dots}\wedge {b}_{p}\wedge {a}_{2}\wedge \mathrm{\dots}\wedge {a}_{m})\sigma $ |
where the body of the chosen clause has replaced ${a}_{1}$ in the answer clause, and the MGU $\sigma $ is applied to the whole answer clause.
An SLD derivation is a sequence of generalized answer clauses ${\gamma}_{0}$, ${\gamma}_{1}$, $\mathrm{\dots}$, ${\gamma}_{n}$ such that
${\gamma}_{0}$ is the answer clause corresponding to the original query. If the query is $q$, with free variables ${V}_{1},\mathrm{\dots},{V}_{k}$, the initial generalized answer clause ${\gamma}_{0}$ is
$yes({V}_{1},\mathrm{\dots},{V}_{k})\leftarrow q.$ |
${\gamma}_{i}$ is obtained by selecting an atom ${a}_{1}$ in the body of ${\gamma}_{i-1}$; choosing a copy of a clause $a\leftarrow {b}_{1}\wedge \mathrm{\dots}\wedge {b}_{p}$ in the knowledge base whose head, $a$, unifies with ${a}_{i}$; replacing ${a}_{1}$ with the body, ${b}_{1}\wedge \mathrm{\dots}\wedge {b}_{p}$; and applying the unifier to the whole resulting answer clause.
The main difference between this and the propositional top-down proof procedure is that, for clauses with variables, the proof procedure must take copies of clauses from the knowledge base. The copying renames the variables in the clause with new names. This is both to remove name clashes between variables and because a single proof may use different instances of a clause.
${\gamma}_{n}$ is an answer. That is, it is of the form
$yes({t}_{1},\mathrm{\dots},{t}_{k})\leftarrow .$ |
When this occurs, the algorithm returns the answer
$${V}_{1}={t}_{1},\mathrm{\dots},{V}_{k}={t}_{k}.$$ |
Notice how the answer is extracted; the arguments to $yes$ keep track of the instances of the variables in the initial query that lead to a successful proof.
Figure 15.5 gives a non-deterministic algorithm that answers queries by searching for SLD derivations. This is non-deterministic in the sense that all derivations can be found by making appropriate choices that do not fail. If all choices fail, the algorithm fails, and there are no derivations. The “choose” on line 13 is implemented using search, as in Example 5.12. Recall that $Unify({a}_{i},a)$ returns an MGU of ${a}_{i}$ and $a$, if there is one, and $\perp $ if they do not unify. The algorithm for $Unify$ is given in Figure 15.4.
The singer Shakira was born in Barranquilla, the capital of Atlántico Department in Colombia. It follows that she was born in Colombia and so also in South America. Consider the following propositions (as part of a larger knowledge base):
$born\mathrm{\_}in(shakira,barranquilla).$ |
$born\mathrm{\_}in(P,L)\leftarrow part\mathrm{\_}of(S,L)\wedge born\mathrm{\_}in(P,S).$ |
$part\mathrm{\_}of(barranquilla,atlantico).$ |
$part\mathrm{\_}of(atlantico,colombia).$ |
$part\mathrm{\_}of(colombia,south\mathrm{\_}america).$ |
A query to ask who is born in Colombia is
$$\text{ask}\text{}born\mathrm{\_}in(P,colombia).$$ |
Figure 15.6 shows a successful derivation with answer $P=shakira$.
$yes(P)\leftarrow born\mathrm{\_}in(P,colombia)$ |
$\text{resolve with:}born\mathrm{\_}in({P}_{1},{L}_{1})\leftarrow part\mathrm{\_}of({S}_{1},{L}_{1})\wedge born\mathrm{\_}in({P}_{1},{S}_{1})$ |
$\text{substitution:}\{{P}_{1}/P,{L}_{1}/colombia\}$ |
$yes(P)\leftarrow part\mathrm{\_}of({S}_{1},colombia)\wedge born\mathrm{\_}in(P,{S}_{1})$ |
select leftmost conjunct |
$\text{resolve with:}part\mathrm{\_}of(atlantico,colombia)$ |
$\text{substitution:}\{{S}_{1}/atlantico\}$ |
$yes(P)\leftarrow born\mathrm{\_}in(P,atlantico)$ |
$\text{resolve with:}born\mathrm{\_}in({P}_{2},{L}_{2})\leftarrow part\mathrm{\_}of({S}_{2},{L}_{2})\wedge born\mathrm{\_}in({P}_{2},{S}_{2})$ |
$\text{substitution:}\{{P}_{2}/P,{L}_{2}/atlantico\}$ |
$yes(P)\leftarrow part\mathrm{\_}of({S}_{2},atlantico)\wedge born\mathrm{\_}in(P,{S}_{2})$ |
$\text{resolve with:}part\mathrm{\_}of(barranquilla,atlantico)$ |
$\text{substitution:}\{{S}_{2}/barranquilla\}$ |
$yes(P)\leftarrow born\mathrm{\_}in(P,barranquilla)$ |
$\text{resolve with:}bor{n}_{i}n(shakira,barranquilla)$ |
$\text{substitution:}\{P/shakira\}$ |
$yes(shakira)\leftarrow $ |
Note that this derivation used two instances of the rule
$$born\mathrm{\_}in(P,L)\leftarrow part\mathrm{\_}of(S,L)\wedge born\mathrm{\_}in(P,S).$$ |
One instance eventually substituted $colombia$ for $S$, and one instance substituted $atlantico$ for $S$.