258 Logic as a Tool Harry are constants/names (interpreted in the familiar domain of humans) The program P consists of the following four facts and two rules: (F1) (F2) (F3) (F4) (R1) (R2) parent(Elisabeth, Charles) parent(Charles, William) parent(Diana, William) parent(William, George) ancestor(X,Y) :- parent(X,Y) ancestor(X,Y) :- parent(X,Z),ancestor(Z,Y) First, consider the following query: (Q0): :- parent(Charles, William) A derivation in the program P Q0 = P + (Q0) consist of a single step, resolving the query (Q0) with the fact (F2) and producing the empty clause, thereby returning the identity substitution, that is, the answer “Yes.” Next, consider the following query: (Q1): :- ancestor(Charles, William) Here is a derivation in the program P Q1 = P + (Q1): first, the substitution [Charles/X, William/Y] unifies the head of (R1) with the body of (Q1), thus resolving these two rules to produce the new query (Q1 ): :- parent(Charles, William) This, again, is immediately derivable, returning again the answer “Yes.” On the other hand, running P on the query (Q2): :- parent(Elisabeth, William) cannot produce the empty clause, simply because none of the rules in P can be resolved with (Q2) since none of their heads can be unified with the only goal in the body of Q2 P will therefore return an answer “No,” which actually means “I don’t know whether parent(Elisabeth, William) holds or not.” This feature of Prolog, known as Negation-by-failure, is discussed further below Now consider the query: (Q3): :- ancestor(Elisabeth, William) A derivation in the program P Q3 = P + (Q3) is as follows (a) The substitution [Elisabeth/X, William/Y] unifies the head of (R2) with the body of (Q3), thus resolving these two rules to produce the new query (Q31): :- parent(Elisabeth,Z), ancestor(Z,William) (b) The substitution [Charles/Z] unifies the fact (F1) with the first goal in the body of (Q31), thus resolving these rules to produce the new query (Q32): :- ancestor(Charles, William)