Information Technology Reference
In-Depth Information
Figure 11.2.
Querying the
est
predicate
?- est([[a],[b],[u,p,b],[p,a]], [b,a]).
Yes
?- est([[a],[b],[u,p,b],[p,a]], [u]).
Yes
?- est([[a],[b],[u,p,b],[p,a]], [p,d]).
No
Figure 11.3.
Tracing the
est
predicate
Call: (5) est([[a],[b],[u,p,b],[p,a]], [u])
Call: (6) est([[a],[b],[u,p,b],[p,a]], [p,b])
Call: (7) est([[a],[b],[u,p,b],[p,a]], [a,b])
Call: (8) est([[a],[b],[u,p,b],[p,a]], [b])
Call: (9) est([[a],[b],[u,p,b],[p,a]], [])
Exit: (9) est([[a],[b],[u,p,b],[p,a]], [])
Exit: (8) est([[a],[b],[u,p,b],[p,a]], [b])
Exit: (7) est([[a],[b],[u,p,b],[p,a]], [a,b])
Exit: (6) est([[a],[b],[u,p,b],[p,a]], [p,b])
Exit: (5) est([[a],[b],[u,p,b],[p,a]], [u])
2.
q
has head
a
and tail
t
, and there is a list in
k
whose head is also
a
and
whose tail is
b
, and
est
k
,
q
)
holds (recursively), where
q
is the list formed
(
by joining
b
and
t
.
This leads to the following two clauses for the
est
predicate:
% est(K,Q): query Q can be established from knowledge base K
est(_,[]).
est(K,[A|T]) :- member([A|B],K), append(B,T,Q), est(K,Q).
In the first clause,
est
holds trivially when the query is empty. In the second clause,
the query is nonempty; it has head
A
and tail
T
; and one must find (using
member
)an
element of the knowledge base
K
whose head is also
A
and whose tail (the body of the
clause) is
B
. To establish the original query, one must be able to establish (recursively)
a new query
Q
that is the result of joining
B
and
T
(using
append
). Note that these two
clauses for
est
form a very concise description of back-chaining.
Some additional examples of using this predicate appear in figure 11.2. It is not
hard to see that this program works just the way Prolog does, that is,
est
(
)
k
,
q
returns