Solution#2

National Taipei University of Technology
Artificial Intelligence (spring, 2013)
Solution # 2
1. What kinds of object are the following Prolog objects (atom, number, variable,
structure)?
(a) Ted  variable
(b) teddy  atom
(c) ‘Tom’  atom
(d)
(e)
(f)
_bts  variable
‘Homework is easy’  atom
easy(homework)  structure
(g)
(h)
(i)
(j)
30  number
holiday(date(28, Feb, 2013))  structure
+(x, y)  structure
dangerous(car, ivy)  structure
2. Please add a new relation relatives(X,Y) based on hw1. (1-2.pl)
Two people are relatives if
(a) One is a predecessor of the other, or
(b) They have a common predecessor, or
(c) They have a common successor
And you must use the semicolon notation to shorten the program.
% 1-2.pl ignore
relatives(X,Y) :predecessor(X, Y) ;
predecessor(Y, X) ;
predecessor(Z, X) , predecessor(Z,Y) ;
%X and Y have a common predecessor
predecessor(X, Z) , predecessor(Y,Z).
%X and Y have a common successor
3. Define the following relation where the line exists in three-dimensional space.
(1) Define the relation parallelX(L) , parallelY(L) and parallelZ(L) where L is
parallel to the X-axis, Y-axis or Z-axis. For example:
parallelX( line( point( _, Y1, Z1), point( _, Y1, Z1))).
parallelY( line( point( X1, _, Z1), point( X1, _, Z1))).
parallelZ( line( point( X1, Y1, _), point( X1, Y1, _))).
(2) Define the relation parallelXY(L) , parallelYZ(L) and parallelXZ(L) where
L is parallel to the XY plane, YZ plane or XZ plane. For example:
parallelXY( line( point( _, _, Z1), point( _, _, Z1))).
parallelYZ( line( point( X1, _, _), point( X1, _, _))).
parallelXZ( line( point( _, Y1, _), point( _, Y1, _))).
4. According to clauses, answer the following question.
Clause 1:The bear is brown.
Clause 2:The cat is black.
Clause 3:The elephant is gray.
Clause 4:The dog is white.
Clause 5:The bear and the elephant are big.
Clause 6:The cat and the dog are small.
Clause 7:Gray and white is bright.
Clause 8:Black and brown is dark.
(1) Write a prolog program distinguishes animal. For example:
brown(bear).
%Clause 1
black(cat).
%Clause 2
gray(elephant).
%Clause 3
white(dog).
%Clause 4
big(bear).
big(elephant).
small(cat).
small(dog).
%Clause 5.1
%Clause 5.2
%Clause 6.1
%Clause 6.2
bright(X):-gray(X).
bright(X):-white(X).
%Clause 7.1
%Clause 7.2
dark(X):-black(X).
dark(X):-brown(X).
%Clause 8.1
%Clause 8.2
(2) According to problem4-1, if your program gets the right answer, please
explain the trace of procedure execute.
QUESTION
?-bright(X),small(X).
X = dog.
EXECUTION TRACE
(1) Initial goal list: bright(X), small(X).
(2) Scan the program from top to bottom looking for a clause whose head
matches the first goal bright(X). Clause 7.1 found: bright(X) :- gray(X).
Replace the first goal by the instantiated body of clause 7.1, giving a
new goal list: gray(X), small(X)
(3) Scan the program to find a match with gray(X). Clause 3 found:
gray(elephant). This clause has no body, so the goal list, properly
instantiated, shrinks to: small(elephant)
(4) Scan the program for the goal small(elephant). No clause found.
Therefore backtrack to step(3) and undo the instantiation X = elephant.
Now the goal list is again: gray(X), small(X)
(5) Continue scanning the program below clause 3. No clause found.
Therefore backtrack to step(2) and continue scanning below clause 7.1.
Clause 7.2 is found:bright(X):-white(X).
Replace the first goal by the instantiated body of clause 7.2, giving a
new goal list: white(X), small(X)
(6) Scan the program to find a match with white(X). Clause 4 found:
white(dog). This clause has no body, so the goal list, properly
instantiated, shrinks to: small(dog)
(7) Scan the program and find clause small(dog) . It has no body so the goal
list shrinks to empty. This indicates successful termination, and the
corresponding variable instantiation is: X = dog