Lab 06 - Prolog - Part 1 & 2
Lab 06 - Prolog - Part 1 & 2
Task No.6
Prolog
Laboratory: Mechatronics
Prelab
What is a knowledge base?
Introduction
A Prolog program consists of a number of clauses. Each clause is either a fact or a rule. After a
Prolog program is loaded (or consulted) in a Prolog interpreter, users can submit goals or
queries, and the Prolog interpreter will give results (answers) according to the facts and rules.
Task Procedure:
girl(sara).
girl(olivia).
girl(khadija).
can_cook(khadija).
Note − Here we have written the name in lowercase letters, because in Prolog, a string starting
with uppercase letter indicates a variable.
Now we can use this knowledge base by posing some queries. “Is sara a girl?”, it will reply
“yes”, “is yusra a girl?” then it will answer “No”, because it does not know who yusra is. Our
next question is “Can Khadija cook?”, it will say “yes”, but if we ask the same question for
Olivia, it will say “No”.
Test the above cases using prolog and take screenshots of each.
2
Task 2: Knowledge Base 2
The facts and rules of the 2nd knowledge base are as follows:
%Facts
can_cook(khadija).
can_cook(yasmin).
can_cook(sana).
%Rules
friends(khadija,yasmin) :- can_cook(yasmin).
friends(khadija,sana) :- can_cook(sana).
Question 1: From your query, list all the members who can cook.
Hint: Suppose we want to see the members who can cook, we can use one variable in our query.
The variables should start with uppercase letters. In the result, it will show one by one. While
using Swish, you can click on “Next” to see the results one by one. Alternatively, you can click
on “10” or the other options to lay down the no. of results accordingly.
- Steve is a thief
- Steve owns a book
- Harry owns a car
- Steve likes to have a car
- Harry likes to have a book
- If the person is a thief and the person likes the item, the item will be stolen.
3
- Who likes what?
- Will Steve steal a car?
- Will Harry steal a car?
Step 1: Build the family tree as specified below in Prolog with the facts and the rules.
Day of Floor of
Name of Time of the Instructor
the the Class Room No.
Course Class Name
Class Location
Add a rule to find out all your instructors for the classes you have specified in the facts.
1) Add facts about average temperatures in Fahrenheit in different countries (around 2-3
countries).
2) Add a rule which will convert the temperature from Fahrenheit to Celsius.
Recall: °C = (°F -32) × 5/9
Run queries to convert the average temperatures in 2 different countries (which are in
Fahrenheit) to Celsius.
-----------------------------------------------------------------------------------------------------------
Note: In the Introduction of your report, include details about what prolog is.
5
6
7