Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Abu Dhabi Polytechnic

Electro-mechanical Engineering Department

EMIS-2101 Introduction to AI Systems (Lab)

Task No.6

Prolog

Laboratory: Mechatronics

Fall Semester, Academic year 2023/2024

Prepared & Supervised By:

Eng. Muhammad Amin Tily


Objective
On completion of this experiment, the student will be able to use a compiler to create a
knowledge base that contains rules and facts and query them using prolog language.

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:

Go to https://swish.swi-prolog.org/ and begin a new program to complete the following tasks.

Task 1: Knowledge Base 1


Suppose we have some knowledge, that Sara, Olivia, and Khadija are three girls, among them,
Khadija can cook. Let’s try to write these facts in a more generic way as shown below:

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.

Question 2: Find out the friends of khadija through the query.

Question 3: Re-write the above rule in an efficient manner.

Task 3: Knowledge Base 3


Create a knowledge base which specifies the following facts:

- Steve is a thief
- Steve owns a book
- Harry owns a car
- Steve likes to have a car
- Harry likes to have a book

The rule will be the following:

- If the person is a thief and the person likes the item, the item will be stolen.

Create queries to show:

- Who owns a book


- What does harry own?

3
- Who likes what?
- Will Steve steal a car?
- Will Harry steal a car?

Task 4 – Family Tree

Step 1: Build the family tree as specified below in Prolog with the facts and the rules.

Step 2: Run queries to find out:


a. The fathers in the family tree
b. The mothers in the family tree
c. Find all grandchildren of John
d. Find all the children of John and Linda

Figure 1 Template for a Family Tree

Task 5 – Multiple Inputs and Writing Text to the Console

Create a program in Prolog which includes the following facts:


- The weather in KSA is hot in summer.
- The weather in KSA is warm in winter.
- The weather in UAE is hot in summer.
- The weather in UAE is warm in winter.
- The weather in England is warm in summer.
- The weather in England is cold in winter.
- The weather in Australia is cold in winter.

Create a rule such that:


4
- It will figure out which countries are hotter than the other country,
- A text should also display on the console a complete sentence such as the following:
UAE is hotter than England

Task 6 – Using Structs


Add the following details in structs in Prolog regarding 3 of your courses:

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.

Run the following queries:


- List all the instructors and classes they are teaching.
- List all your classes on Wednesdays (or pick another day in which you have a class in
your list of facts).
- Is there any class scheduled on any day between 1300 and 1500 (only display the course
name)?

Task 7 – Performing Math Operations

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

You might also like