PROLOG PROGRAMMING - PRACTICAL ASSIGNMENT
JOSEPH MUNDIA BITC01/2154/2022
(1) For below facts, rules, and goals write English Meanings.
(a) color(carrots, orange). The fact that the color of carrots is orange
(b) likes(Person, carrots):-vegetarian(Person). The rule that a person likes carrots if they are a
vegetarian.
(c) pass(Student) :- study_hard(Student). The rule that a student will pass if they study hard.
(d) ?- pass(Who). The goal to find who passes.
(e) ?- teaches(professor, Course). The goal to find what course a professor teaches.
(f) enemies(X, Y) :- hates(X, Y), fights(X, Y). The rule that X and Y are enemies if X hates Y and they fight.
(2) For below english sentences write applicable Prolog facts, rules & goals.
(a) Maria reads logic programming book by author peter lucas.
reads(maria, 'logic programming book', 'peter lucas').
(b) Anyone likes shopping if she is a girl. likes(X, shopping) :- girl(X).
(c) Who likes shopping? ?- likes(Who, shopping).
(d) kirke hates any city if it is big and crowdy. hates(kirke, City) :- big(City), crowdy(City).
(3) (a) Write a simple prolog program that computes cube for the number.
cube(A, B) :- B is A * A * A.
(b) Try to read number from the query prompt and find cube.
?- cube(A, B), write('The cube of '), write(A), write(' is '), write(B), nl.
(4) Find flaws in following clauses.
(1) hates(X,Y), hates(Y,X) :- enemies(X, Y)
The clause hates(X,Y), hates(Y,X) :- enemies(X, Y) is incorrect. It should be enemies(X, Y) :- hates(X, Y),
hates(Y, X).
(2) p(X):-(q(X):-r(X)).
The clause p(X):-(q(X):-r(X)). is incorrect. It should be p(X) :- q(X), r(X).
(5) For given English statements write a prolog program
- Facts & Rules
(1) jia is a woman.
(2) john is a man.
(3) john is healthy.
(4) jia is healthy.
(5) john is wealthy.
(6) anyone is a traveler if he is healthy and wealthy.
(7) anyone can travel if he is a traveler.
- Goals.
(1) Who can travel?
(2) Who is healthy and wealthy?
% Facts
woman(jia).
man(john).
healthy(john).
healthy(jia).
wealthy(john).
% Rules
traveler(X) :- healthy(X), wealthy(X).
can_travel(X) :- traveler(X).
% Goals
?- can_travel(X).
?- healthy(X), wealthy(X).
(6) What answers do you get for below queries for given program.
Program :
vegetarian(jose).
vegetarian(james).
vegetable(carrot).
vegetable(egg_plant).
likes(jose, X) :- vegetable(X).
loves(Who, egg_plant) :- vegetarian(Who).
Queries :
?- vegetable(X).
?- vegetable(potato).
?- vegetarian(_).
?- likes(jose, What).
?- likes(Who, egg_plant).
?- loves(Who, egg_plant).
?- vegetable(X).
X = carrot ;
X = egg_plant.
?- vegetable(potato).
false.
?- vegetarian(_).
true ;
true.
?- likes(jose, What).
What = carrot ;
What = egg_plant.
?- likes(Who, egg_plant).
Who = jose.
?- loves(Who, egg_plant).
Who = jose ;
Who = james.
(7) For given program how would prolog respond to the query if you keep entering ';'
after each solution?
Program :
p(a,b).
p(b,c).
p(X, Y):-p(Y, X).
Query :
?- p(X,Y).
(hint : Recursion in prolog. - Recursion)
?- p(X,Y).
X = a, Y = b ;
X = b, Y = c ;
X = c, Y = b ;
X = b, Y = a ;
false.