Header
% =================================================================
% == ==
% == An Introduction to ARTIFICIAL INTELLIGENCE ==
% == Janet Finlay and Alan Dix ==
% == UCL Press, 1996 ==
% == ==
% =================================================================
% == ==
% == chapter 1, page 16: representing logic ==
% == ==
% == Prolog example, Alan Dix, August 1996 ==
% == ==
% =================================================================
Code
% The family relationships for this example are drawn from the
% Thunderbirds television series. See the web pages for more
% information about Thunderbirds.
father(jeff,virgil). % Note the use of lower case names
father(jeff,alan). % If we wrote 'father(Jeff,Alan)' in Prolog
father(jeff,scott). % the upper case names 'Jeff' and 'Alan'
father(jeff,john). % would be treated as variables, just as if
father(jeff,gordon). % we had written 'father(X,Y)'. That is we
mother(grandma,jeff). % we would have said that everybody was
father(kyrano,tin_tin). % everybody else's father!
sibling(kyrano,hood). % Kyrano and the Hood are declared as siblings as
sibling(hood,kyrano). % we do not know who their father or mother is.
% This shows how Prolog allows you to mix declared
% facts such as these with rules (below) for the
% same predicate.
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
sibling(Y,Z) :- parent(X,Y), parent(X,Z).
% There are two facts given about the Hood and Kyrano.
% Why don't I have rule saying 'sibling(Y,Z) :- sibling(Z,Y)',
% as this would have allowed us to deduce 'sibling(hood,kyrano)'
% from the fact 'sibling(kyrano,hood)'?
% If you aren't sure, try adding this rule and see what happens!
Running this Code
% RUNNING THIS CODE
%
% When testing any program, always remember to try test cases of both
% things you expect to work: e.g. 'sibling(alan,john)'
% and those you don't: e.g. 'sibling(hood,grandma)'
%
% Also with Prolog you can try asking it questions with variables
% in such as: 'sibling(scott,X)'
%
% You may get a surprise, among the answers you will get
% 'sibling(scott,scott)'. Actually this makes perfect sense.
% 'jeff' is the parent of 'scott' and so by the rule 'scott'
% is his own sibling.
%
% To see all the sibling pairs try asking: 'sibling(X,Y)'
% Again you will see all the reflexive pairs 'sibling(virgil,virgil' etc.
% and also you will see answers both ways round for each pair.
Examples
% EXAMPLES
%
%> sibling(alan,john). % expected to work
%
%> sibling(hood,grandma). % and not
%
%> sibling(scott,X). % with variable
%
%> sibling(scott,scott). % some surprises ... what might 'fix this?
%
%> sibling(X,Y). % see everything