Lab 1
Lab 1
An Introduction to Prolog
PRACTICAL 01
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.
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.
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:
(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(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:
Undefined goal /* No. In the database above we only know about the relation */
/* age, not about the relation bilal, so the query fails */
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.
?- 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).
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
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).