CPCS202 - The Lab Note

Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
Term I
2013
Statement Outcome
Demonstrate Compound queries with multiple variables, facts containing variables, and to understand about the Rules
in prolog.
Activity Outcomes
Students will understand how to execute Compound queries with multiple variables, facts containing variables, and to
understand about the Rules in prolog with suitable examples.
Instructor Note
Solve the exercise given at the end of the notes and submit.
Notes
Compound queries with multiple variables
• To find persons who like more than one game:
likes(Person, G1),likes (Person,G2),G1<>G2.
• To find games liked by more than one person:
likes (P1,Game), likes(P2,Game),P1<>P2.
Facts containing variables
• Assume we add the drinks facts to the likes database as follows:
PREDICATES
drinks(symbol, symbol)
CLAUSES
drinks(ali, pepsi).
drinks (samir, lemonada).
drinks (ahmad, milk).
• To add the fact that all persons drink water:
drinks (Everyone, water).
• If we put a goal like:
CPCS-331 - The Lab Note
Lab-3
Term I
2013
Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
drinks (samy,water).
drinks (ahmad,water).
Output:
CPCS-331 - The Lab Note
Lab-3
Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
Term I
2013
Rules
• Rules are used to infer new facts from existing ones.
• A rule consists of two parts: head and body separated by the symbol
:- .
• To represent the rule that express the facts that two persons are friends
if they both like the same game:
friends( P1,P2):-
likes (P1,G),likes (P2,G),P1<>P2.
Head of the rule
(Here P1 and P2 are friends, due to P1 and P2 likes the same Game.)
Exercise
1. Declare your own predicates and clauses and write a complete prolog program to implement the rule given
below.
friends( P1,P2):likes (P1,G),likes (P2,G),P1<>P2.
Hint: find all friends.
2.Assume the following “likes” facts knowledge base
likes (ali, football).
likes (ali, tennis).
likes (ahmad, tennis).
likes (ahmad, handball).
likes (samir, handball).
likes (samir, swimming).
likes (khaled, horseriding).
Find all the people who like more than one game.?
CPCS-331 - The Lab Note
Lab-3
Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
Term I
2013
3. Assume the following “likes” facts knowledge base
likes (ali, tennis).
likes (samir, football).
likes (ahmad, baseball).
likes (khaled, swimming).
likes (anwar, tennis).
Add a rule to the following knowledge base which shows “majid likes an activity if ahmad likes that activity”
Then answer the following questions.
Q1. likes(majid, baseball).
Q2. likes(majid, tennis).
Provide your reasons for both the answers.
CPCS-331 - The Lab Note
Lab-3
Term I
2013
Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
Solution
OUTPUT
CPCS-331 - The Lab Note
Lab-3
Term I
2013
Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
Example:
Output:
CPCS-331 - The Lab Note
Lab-3
Term I
2013
Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
The rule to be added, where
Activity is a variable.
answer to the first is yes and the scond question will be no.
CPCS-331 - The Lab Note
Lab-3
Term I
2013
Lab3: compound queries with multiple variables using
visual PROLOG version 5.2
Bonus Question
A person can buy a car if the person likes the car and the car is for sale.
This natural language relationship can be conveyed in Prolog with the following rule:
can_buy(Name, Model):person(Name),
car(Model),
likes(Name, Model),
for_sale(Model).
This rule shows the following relationship:
Name can_buy Model if
Name is a person and
Model is a car and
Name likes Model and
Model is for sale.
Write a program to give the solution for the car-buying problem.
Hint: the predicates are given, you have to write the facts and
rules.
PREDICATES
nondeterm can_buy(symbol, symbol)
nondeterm person(symbol)
nondeterm car(symbol)
likes(symbol, symbol)
for_sale(symbol)
then answer the following questions,
can_buy(Who, What).
can_buy(ali, What).
can_buy(ahmad, What).
can_buy(Who, hot_rod).
CPCS-331 - The Lab Note
Lab-3