12.3.3 Queries with Variables

Queries are used to ask whether something is a logical consequence of a knowledge base. With propositional queries, a user can ask yes-or-no questions. Queries with variables allow the system to return the values of the variables that make the query a logical consequence of the knowledge base.

An instance of a query is obtained by substituting terms for the variables in the query. Different occurrences of a variable must be replaced by the same term. Given a query with free variables, an answer is either an instance of the query that is a logical consequence of the knowledge base, or "no", meaning that no instances of the query logically follow from the knowledge base. Instances of the query are specified by providing values for the variables in the query. Determining which instances of a query follow from a knowledge base is known as answer extraction.

% imm_west(W,E) is true if room W is immediately west of room E.


% imm_east(E,W) is true if room E is immediately east of room W.

imm_east(E,W) ←

% next_door(R1,R2) is true if room R1 is next door to room R2.

next_door(E,W) ←
next_door(W,E) ←

% two_doors_east(E,W) is true if room E is two doors east of room W.

two_doors_east(E,W) ←
     imm_east(E,M) ∧

% west(W,E) is true if room W is west of room E.

west(W,E) ←
west(W,E) ←
     imm_west(W,M) ∧
Figure 12.2: A knowledge base about rooms

Example 12.12: Consider the clauses of Figure 12.2. The person who wrote these clauses presumably has some meaning associated with the symbols, and has written the clauses because they are true in some, perhaps imaginary, world. The computer knows nothing about rooms or directions. All it knows are the clauses it is given; and it can compute their logical consequences.

The user can ask the following query:

ask imm_west(r105,r107).

and the answer is yes. The user can ask the query

ask imm_east(r107,r105).

and the answer is, again, yes. The user can ask the query

ask imm_west(r205,r207).

and the answer is no. This means it is not a logical consequence, not that it is false. There is not enough information in the database to determine whether or not r205 is immediately west of r207.

The query

ask next_door(R,r105).

has two answers. One answer, with R=r107, means next_door(r107,r105) is a logical consequence of the clauses. The other answer is for R=r103. The query

ask west(R,r105).

has two answers: one for R = r103 and one for R = r101. The query

ask west(r105,R).

has three answers: one for R = r107, one for R = r109, and one for R = r111. The query

ask next_door(X,Y).

has 16 answers, including

X = r103, Y = r101
X = r105, Y = r103
X = r101, Y = r103