Information Technology Reference
In-Depth Information
Figure 5.17.
A solution to the zebra problem
zebra.pl
% This is a solution to the classic zebra puzzle.
solution(Zebra,England,Spain,Ukraine,Japan,Norway) :-
% The fourteen clues
England=Red, Spain=Dog, Coffee=Green, Ukraine=Tea,
left(Ivory,Green), Winston=Snail, Kool=Yellow,
middle_pos(Milk), leftmost_pos(Norway),
next_to(Chesterfield,Fox), next_to(Kool,Horse),
LuckyStrike=OJ, Japan=Parliament, next_to(Norway,Blue),
% The five lists: houses, nations, pets, drinks, cigarettes
uniq_pos(Green,Red,Yellow,Ivory,Blue),
uniq_pos(England,Spain,Ukraine,Japan,Norway),
uniq_pos(Dog,Snail,Zebra,Fox,Horse),
uniq_pos(Tea,Milk,OJ,Coffee,OtherDrink),
uniq_pos(Winston,Kool,Parliament,Chesterfield,LuckyStrike).
%-------------- The positional predicates -------------------
uniq_pos(P1,P2,P3,P4,P5) :-
pos(P1), pos(P2), pos(P3), pos(P4), pos(P5),
\+ P1=P2, \+ P1=P3, \+ P1=P4, \+ P1=P5, \+ P2=P3,
\+ P2=P4, \+ P2=P5, \+ P3=P4, \+ P3=P5, \+ P4=P5.
pos(1).
pos(2).
pos(3).
pos(4).
pos(5).
leftmost_pos(1).
middle_pos(3).
left(1,2).
left(2,3).
left(3,4).
left(4,5).
next_to(X,Y) :- left(X,Y).
next_to(X,Y) :- left(Y,X).
positional constraints suggest that values be taken from a positional ordering of the five
houses: 1 (leftmost), 2 (left middle), 3 (middle), 4 (right middle), 5 (rightmost). The
knowledge base can include facts about which position is leftmost, which is middle,
which position is directly to the left of another, and which position is next to another.
These positional facts together with the clause for the solution predicate (from the
fourteen puzzle clues and the uniqueness constraints) are shown in figure 5.17.
Since the values of all the variables are just positional numbers, it takes some extra
work to get a more descriptive answer. A query with the solution predicate brings
the following result:
?- solution(Zebra,E,S,U,J,N).
Zebra = 5,
E = 3,
S = 4,
U = 2,
J = 5,
N = 1
Yes
Search WWH ::




Custom Search