CPCS202 - The Lab Note

Lab2: Using visual PROLOG
Term I
2013
`
Statement Outcome
Discussions about predicate, clauses and goal sections in prolog, how to supply a query in prolog, how to execute a
query with Variable, Compound queries with one variable.
Activity outcomes
Students will understand how to write a prolog program (predicates, clauses and goal) and how to execute a simple
query, query with a variable, Compound queries with one variable.
Instructor Note:
Solve the exercise given at the end of the notes and submit.
Notes
Getting started with prolog
 Go to: start menu  all programs visual prolog 5.2 personal edition vip32.
Click on file and select new
CPCS-331 - The Lab Note
Lab-2
Lab2: Using visual PROLOG
Term I
2013
`
Type the program in the white space
To represent the likes facts in VPROLOG
• FileNewnoname.pro
• Then in the predicate section write the declaration of the used predicates:
PREDICATES
nondeterm likes (symbol,symbol)
• Then in the clauses section write the facts:
CLAUSES
likes (ali,football).
likes (ali,tenis).
likes (ahmad,tenis).
likes (ahmad,handball).
likes (samir,handball).
CPCS-331 - The Lab Note
Lab-2
Lab2: Using visual PROLOG
Term I
2013
`
likes (samir,swimming).
likes (khaled,horseriding).
Queries as goals in PROLOG
• To supply a query in PROLOG put it in the goal section as follows:
GOAL
likes (ali, football).
• Then press Cntrl+G
The output will be Yes or No for concrete questions
1.
Concrete questions: (queries without variables)
Example:
GOAL
likes(samir, handball).
Yes
Likes (samir,football).
No
Procedure
CPCS-331 - The Lab Note
Lab-2
Term I
2013
Lab2: Using visual PROLOG
`
Then click on G to run the goal. The result is ‘yes’ as ali likes football.
CPCS-331 - The Lab Note
Lab-2
Lab2: Using visual PROLOG
Term I
2013
`
2. Queries with variables
• To know all sports that ali likes:
likes(ali,What). OR you can type likes(ali, X). X is a variable.
• To know which person likes teniss
likes (Who,tenis).
• To know who likes what (i.e all likes facts)
likes (Who,What). Or you can type likes(X,Y). where X is Who and Y is What . Variable
must be in upper case or first letter of variable must be in upper case.
3.Compound queries with one variable
1. To list persons who likes tennis and football, the goal will be
likes( Person, tenis ),likes (Person, football).
2. To list games liked by ali and ahmad
CPCS-331 - The Lab Note
Lab-2
Lab2: Using visual PROLOG
Term I
2013
`
likes (ali,Game),likes (ahmad,Game).
Exercise:
1. List all the game which ali likes.
2. Find all the persons who likes cricket.
3. List all the people and the games they like.
4. Find the games like by ali and ahmed.
CPCS-331 - The Lab Note
Lab-2