Information Technology Reference
In-Depth Information
Figure 5.15.
A solution to the first logic problem
logic1.pl
% A logic puzzle involving jobs and musical instruments,
solution(Flute) :-
% Distinct occupations and instruments
uniq_people(Doctor,Lawyer,Engineer),
uniq_people(Piano,Violin,Flute),
% The four clues
\+ chris = Doctor, % Chris is married to the doctor.
Lawyer = Piano, % The lawyer plays the piano.
\+ Engineer = chris, % The engineer is not Chris.
Violin = Doctor, % Sandy is a patient of
\+ sandy = Violin. % the violinist.
% uniq(...) is used to generate three distinct people.
uniq_people(A,B,C) :- person(A),
person(B),
person(C),
\+ A=B, \+ A=C, \+ B=C.
% The three given people
person(chris).
person(sandy).
person(pat).
Putting such facts into a knowledge base is tricky, since the conclusions involve equal-
ity. For present purposes, it is much easier to embed the information directly in the
solution predicate, as shown in figure 5.15. The rest of the program is similar to
previous examples.
5.5.1 Hidden variables
The main challenge in logic problems like these is typically to determine how to
express the constraints. Often the only way to do this is to imagine that there are
hidden variables that are not mentioned but whose values need to be taken into account.
This was the case with the carry digits in the cryptarithmetic problems. They had to
be introduced in order to express the constraints among the mentioned digits.
For example, suppose there is exactly the same logic problem as before, except that
constraint 3 is
3.
Pat is not married to the engineer.
How should this piece of information be handled? At first it seems that it places no
constraint on either Pat or the engineer. However, it is useful to think in terms of new
variables, for Pat's spouse, Chris's spouse, and Sandy's spouse. Assuming they can
only be married to each other, the domain for these variables will be the three people
( sandy , chris , pat )or none , to handle the case where the person is not married. The
Search WWH ::




Custom Search