So how does Prolog find the answer?
The deductive process is sometimes called resolution. Prolog
takes what you type and tries to make it go away.
You type a list of comma-separated clauses, and it tries to
unify the left one. That is, it tries to find variable assignments
which make the left clause match the left side of some rule.
If it does, it substitutes the right side of the rule for the
clause, with the variables substituted according to what made the
resolution. Repeat.
The rule will have zero or more clauses on the right, so after
replacement, the number of clauses the resolver must deal with
may increase or decrease. The goal is to reduce it to zero. It will
then have proved that the original query follows from the rules,
with the variable substitutions made. Giving a variable a value is
called resolving it.
Here is an extended example using the
family.pl example.
consult(family).
brother_in_law(B,lola).
X=B
Y=lola
brother(B, Z), spouse(Z, lola)
X2=B
Y2=Z
male(B),sibling(B,Z),spouse(Z,lola).
B=dave
sibling(dave,Z),spouse(Z,lola).
X3=dave
Y3=Z
mother(Z2,dave),mother(Z2,Z), dave \== Z, spouse(Z, lola)
X4=Z2
Y4=dave
female(Z2), parent(Z2, dave),mother(Z2,Z), dave \== Z, spouse(Z, lola)
Z2=lola
parent(lola, dave),mother(lola,Z), dave \== Z, spouse(Z, lola) [fail]
female(Z2), parent(Z2, dave),mother(Z2,Z), dave \== Z, spouse(Z, lola)
Z2=bonnie
...
female(Z2), parent(Z2, dave),mother(Z2,Z), dave \== Z, spouse(Z, lola)
Z2=phyl
parent(phyl, dave),mother(phyl,Z), dave \== Z, spouse(Z, lola)
mother(phyl,Z), dave \== Z, spouse(Z, lola)
X5=phyl
Y5=Z
female(phyl),parent(phyl,Z), dave \== Z, spouse(Z, lola)
parent(phyl,Z), dave \== Z, spouse(Z, lola)
Z=tom
dave \== tom, spouse(tom, lola)
spouse(tom, lola)
XX=tom
YY=lola
husband(tom, lola)
[succeed] ;
spouse(tom, lola) [fail]
parent(phyl,Z), dave \== Z, spouse(Z, lola)
Z=dave
dave \== dave [fail]
mother(phyl,Z), X3 \== Y3, spouse(Z, lola) [fail]
parent(phyl, dave),mother(phyl,Z), dave \== Z, spouse(Z, lola) [fail]
female(Z2), parent(Z2, dave),mother(Z2,Z), dave \== Z, spouse(Z, lola)
Z2=rachel
parent(rachel, dave),mother(rachel,Z), dave \== Z [fail], spouse(Z, lola)
female(Z2), parent(Z2, dave),mother(Z2,Z), dave \== Z [fail], spouse(Z, lola)
mother(Z2,dave),mother(Z2,Z), dave \== Z [fail], spouse(Z, lola)
sibling(dave,Z),spouse(Z,lola) [fail]
male(B),sibling(B,Z),spouse(Z,lola).
B=tom
sibling(tom,Z),spouse(Z,lola).
...
male(B),sibling(B,Z),spouse(Z,lola). [fail]
...
brother(B, Z), spouse(Z, lola) [fail]
brother_in_law(B,lola).
X6=B
Y6=lola
husband(B,Z3), sister(Z3, lola).
B=tom
Z3=lola
sister(lola,lola)
... [fail]
husband(B,Z3), sister(Z3, lola).
B=dennis
Z3=carrie
sister(carrie,lola)
X7=carrie
Y7=lola
female(carrie), sibling(carrie, lola)
sibling(carrie, lola)
X8=carrie
Y8=lola
mother(Z4,carrie),mother(Z4,lola),carrie \= lola
X9=Z4
Y9=carrie
female(Z4), parent(Z4,carrie),mother(Z4,lola),carrie \= lola