0% found this document useful (0 votes)
152 views

Lab 1

The document provides an introduction to Prolog and artificial intelligence. It outlines the course objectives which include learning about facts, variables, backtracking, recursion, lists, searching algorithms, and implementing problems like the Zebra problem and Tower of Hanoi. It describes how Prolog is a logic programming language based on facts and rules, and how it is commonly used for artificial intelligence and expert systems. It also provides information on evaluating student performance and introduces key Prolog concepts like facts, arguments, variables, and unification.

Uploaded by

beenish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Lab 1

The document provides an introduction to Prolog and artificial intelligence. It outlines the course objectives which include learning about facts, variables, backtracking, recursion, lists, searching algorithms, and implementing problems like the Zebra problem and Tower of Hanoi. It describes how Prolog is a logic programming language based on facts and rules, and how it is commonly used for artificial intelligence and expert systems. It also provides information on evaluating student performance and introduces key Prolog concepts like facts, arguments, variables, and unification.

Uploaded by

beenish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Artificial Intelligence

An Introduction to Prolog

PRACTICAL 01

Engr. Beenish Qureshi


Course Objectives (1)
1) Introduction to prolog and understanding
facts.
2) Understanding facts with arguments and
variables.
3) Understanding backtracking and recursion.
4) Defining relations by facts, by Rules and
Recursive rules.
5) To become familiar with list in Prolog and
operations on list.
6) Write and Execute prolog program for BFS.
Course Objectives (2)
7) Write and Execute prolog program for
DFS.
8) Implementing Basic AI Searching
techniques in Prolog.
9) Finding a Telephone in a room.
10) To solve Tower of Hanoi Puzzle.
11) Using Structures, To solve Zebra
Problem.
12) To get familiar with Expert System.
Evaluation and Grading
 Class Behavior/Response 05
 Attendance 05
 Project 10
 Lab Manual 10
 Total 30
S. no Percentage of practical Marks to be
attended awarded
1 95% - 100% 5
2 86% - 94% 4
3 81% - 85% 3
4 75% - 80% 2
5 Below 75% 0
Introduction to Prolog
Artificial Intelligence
PROLOG
 Short for PROgramming LOGic
 Prolog is a high-level programming language based on
formal logic.
 Prolog is sometimes called a declarative language or a
rule-based language because its programs consist of a
list of facts and rules.
 Prolog is used widely for artificial intelligence
applications, particularly expert systems.
 An expert system is a computer system that emulates
the decision-making ability of a human expert
Brief History
 The first, official version of Prolog was
developed
◦ at the University of Marseilles, France
◦ by Alain Colmerauer
◦ in the early 1970s
◦ as a tool for PROgramming in LOGic.
Application Areas
 Prolog has been a very important tool in
◦ artificial intelligence applications
◦ expert systems
◦ Databases: query languages, data mining,…
◦ Mathematics: theorem proving
Declarative Language
 This means that the programmer declares facts and
defines rules for reasoning with the facts
 Rather than describing how to compute a solution, a
program consists of a data base of facts and logical
relationships (rules).
 Rather than running a program to obtain a solution, the
user asks a question. When asked a question, the run
time system searches through the data base of facts and
rules to determine (by logical deduction) the answer.
SWI-Prolog
 We will use “SWI-Prolog” for the Prolog programming assignment
 SWI-Prolog has been under continuous development since 1987.
 Its main author is Jan Wielemaker.
 The name SWI is derived from Social-Wetenschappelijke Informatica
("Social Science Informatics"), the former name of the group at the
University of Amsterdam, where Wielemaker is employed.
Link to download:
 http://www.swi-prolog.org/
 Click on
SWI-Prolog
 From available versions, Click on Stable release

 Click on the “SWI –Prolog 6.6.1, and the downloading process starts

 Install SWI-Prolog.
A Prolog Program
 The program, sometimes called Database is a text file (*.pl) that
contain the facts and rules. It contains all the relations needed to
define the problem.
 The program is created in SWI Prolog and save it with *.pl
extension.

 When you launch a program you are in query mode represented


by the ? – prompt. In query mode you ask questions about the
relations described in the program.

 In SWI-Prolog you can load a program by typing the command [file]


or consult(file). when the file containing your program is file.pl.
When you have done this you can use all the facts and rules that
are contained in the program.
Simple Facts
 In Prolog we can make some statements by using facts.
 Facts either consist of a particular item or a relation
between items.
 For example we can represent the fact that it is sunny by
writing the program:
sunny.
rainy.
 We can now ask a query of Prolog by asking
?- sunny.
True
Simple Facts
 ?- is the Prolog prompt.To this query, Prolog will answer true.
 sunny is true because (from above) Prolog matches it in its database
of facts.
Simple Rules of Syntax:
 Facts should always begin with a lowercase letter and end with a full
stop.
 The facts themselves can consist of any letter or number combination,
as well as the underscore _ character.
 However, names containing the characters -,+,*,/, or other
mathematical operators should be avoided.
Simple Facts
Here are some simple facts about an imaginary world. /* and */ are
comment delimiters
 ali_is_bold. /* ali is bold */
 raining. /* it is raining */
 ali_Forgot_His_Raincoat. /* ali forgot his raincoat */
 ahmed_lost_his_car_keys. /* ahmed lost his car keys */
 umair_footballer. /* umair plays football */

 These describe a particular set of circumstances for some characters.


What makes a fact a fact?
Examples:
 Exams exams.
 Assignments assignments.
 Taxes taxes.
 The earth is round round(earth).
 The sky is blue blue(sky).
 The sun is hot hot(sun).
 Sana is female female(sana).
Simple Fact Exercises
Exercise#1:
 Which of the following are syntactically correct facts (indicate
yes=correct, no=incorrect)

1. Islamabad.
2. tomsRedCar.
3. 2Ideas.
4. Prolog.
Simple Fact Exercises
Exercise#2
 Create the database given below:
1. blue_box.
2. red_box.
3. green_circle.
4. blue_circle.
5. orange_triangle.

Execute the following queries:


 ?- green_circle.
 ?- circle_green.
 ?- red_triangle.
 ?- red_box.
 ?- orange_Triangle.
Simple Fact Exercises
Exercise#3
Convert the following into predicates:
1.Ali is a man.
2.dog barks.
3.butterfly flies.
4.human are mortal.
5.Murtaza was boxer.
6.wali grows.
7.saeed runs.
8.pigeon is a bird.
9.all birds flies.
10.a car has four wheels.
PRACTICAL # 02
Understanding Facts with arguments and
Variables.

Engr. Beenish Adeel.


Facts with Arguments
 More complicated facts consist of a relation and the items that this
refers to.
 These items are called arguments.
 Facts can have arbitrary number of arguments from zero upwards.

 A general model is shown below:


 relation(<argument1>,<argument2>,....,<argumentN> ).

 Relation names must begin with a lowercase letter


 likes(john,chocolates).
Facts with Arguments
Exercise#

 Create the database given below:

1. eats(ahmed,oranges).
2. eats(ali,apples).
3. eats(umair,oranges).
Facts with Arguments
If we now ask some queries we would get the following interaction:

 ?- eats(ahmed,oranges). /* does this match anything in the database? */

true /* yes, matches the first clause in the database */

 ?- eats(ali,apples). /* do we have a fact that says ali eats apples? */

true /* yes we do, clause 4 of our eats database */

 ?- eats(saba,apples). /* how about this query, does saba eat apples */

(undefined goal )false /* not according to the above database. */

 ?- eats(sana,apples). /* does sana eat apples */

(undefined goal )false /* again no, we don't know whether sana eats apples */
Facts with Arguments
 Let us consider another database. This time we will use the predicate
age to indicate the ages of various individuals.

 age(adnan,32). /* adnan is 32 years old */

 age(bilal,41). /* bilal is 41 */

 age(sumaira,72). /* sumaira is 72 */

 age(nikki,2). /* nikki is 2 */

 age(aqsa,25). /* aqsa is 25 */
Facts with Arguments
If we now ask some queries we would get the following interaction:

 ?- age(nikki,2). /* is nikki 2 years old? */

true /* yes, matches against the fourth clause of age */

 ?- bilal(41). /* for some relation bilal are they 41 */

Undefined goal /* No. In the database above we only know about the relation */
/* age, not about the relation bilal, so the query fails */

 ?- age(nikki,two) /* is nikki two years old? */

false /* No. two and 2 are not the same and therefore don't match */
Example
Program

location(desk, office).
location(apple, kitchen). Query
location(flashlight, desk).
location('washing machine', cellar). ?- location(apple, kitchen).
true
location(powder, 'washing machine').
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).
Variables and Unification
 How do we say something like "What does Fred eat"? Suppose we
had the following fact in our database:

 eats(fred,mangoes).
 How do we ask what fred eats. We could type in something like
 ?- eats(fred,what).
 However Prolog will say false.
 The reason for this is that what does not match with mangoes.
 In order to match arguments in this way we must use a Variable.
Variables and Unification
 The process of matching items with variables is known as unification.
 Variables are distinguished by starting with a capital letter.

Here are some examples:


 X /* a capital letter */
 VaRiAbLe /* a word - it be made up or either case of letters */
 My_name /* we can link words together via '_' (underscore) */
Variables and Unification
 Thus returning to our first question we can find out what fred eats by
typing

 ?- eats(fred,What).
 What=mangoes

 As a result of this query, the variable What has matched (or unified)
with mangoes.
 We say that the variable What now has the binding mangoes.
 When we pose a query, if the query is successful, Prolog prints both
the variable and the variable name, as we see above.
Variables and Unification
 Let's consider some examples using facts. First consider the following
database.
 loves(john,travelling).
 loves(fred,hobbies).

Simple queries using variables:


 ?- loves(john,Who). /* Who does john love? */
 Who=travelling /* yes , Who gets bound to travelling */

 ?- loves(arnold,Who) /* does arnold love anybody */


 false /* no, arnold doesn't match john or fred */

 ?- loves(fred,Who). /* Who does fred love */


 Who = hobbies /* Note the to Prolog Who is just the name of a
variable, */
Variables and Unification
Exercise:
 Consider the following database showing a library of cassette tapes.
Notice the final argument to tape, which is the name of a favorite
song from the album 5.

1. tape(1,van_morrison,astral_weeks,madam_george).
2. tape(2,beatles,sgt_pepper,a_day_in_the_life).
3. tape(3,beatles,abbey_road,something).
4. tape(4,rolling_stones,sticky_fingers,brown_sugar).
5. tape(5,eagles,hotel_california,new_kid_in_town).
Variables and Unification
/* what are the contents of tape 5 */
 ?- tape(5,Artist,Album,Fave_Song).
Artist=eagles
Album=hotel_california
Fave_Song=new_kid_in_town

/* find just song */


 ?- tape(4,rolling_stones,sticky_fingers,Song).
Song=brown_sugar /* which you like best from the album */
Exercise
 Which of the following sequences of
characters are atoms, which are variables,
and which are neither?
 gRACIOUS.
 Landmark
 variable123
 Variable2000
 ‘big_cheese_burger’
 big_cheese_sandwitch
 Ali
 _Wali
 ‘_Saud’
Rules
 Rules allow us to make conditional statements about our world. Each rule
can have several variations, called clauses.
 These clauses give us different choices about how to perform inference
about our world. Consider the following:
 'All men are mortal':
 We can express this as the following Prolog rule
 mortal(X) :-human(X).
 The clause can be read in two ways (called either a declarative or a
procedural interpretation).
 The declarative interpretation is "For a given X, X is mortal if X is human."
 The procedural interpretation is "To prove the main goal that X is mortal,
prove the subgoal that X is human."
Rules
 To continue our previous example, lets us define the fact 'Socrates is human'
so that our program now looks as follows:

 mortal(X) :-human(X).
 human(socrates). If we now pose the question to Prolog

 ?- mortal(socrates).
 true
 This said that in order to prove someone mortal, we had to prove them to be
human. Thus from the goal Prolog generates the subgoal of showing
human(socrates).
 In the above example we were able to match human(socrates) against the
database. In Prolog we say that the subgoal succeeded, and as a result the
overall goal succeeded.
Rules
 Rules are also used to express definitions,
for Example:
◦ X is a bird if:
 X is an animal, and
 X has feathers.
or
◦ X is sister of Y if:
 X is female, and
 X and Y have the same female.
Example
 animal(x).
 feathers(x).
 bird(x) :- animal(x), feathers(x).
 Eats(ali,apples).
 Eats(ahmed,apples).
 Eats(sana,apples).
 Eats(sami,apples).

You might also like