You are on page 1of 26

Project file

of
Prolog Language

A Minor Project‟s Report in fulfillment of the requirement of

Degree of Master of Science in Information Technology

M.Sc -IT (2nd Semester)

of

Panjab University , Chandigarh.

Submitted To:- Submitted By:-

Prof. Bohar Singh Bhavneet Kaur

Roll No:-3806

G.H.G. Khalsa College


Gurusar Sadhar, Ludhiana.

1
Acknowledgment

I would like to take this opportunity in expressing my deepest gratitude to all those
persons who in way or other helped me in making my endeavors at G.H.G. Khalsa
College Gurusar Sadhar, Ludhiana a success.

It is my privilege to express my sincerest regards to our advisor, Prof. Bohar Singh,


for their valuable inputs, able guidance, encouragement, whole-hearted cooperation
and constructive criticism throughout the duration of my work.

I deeply express my sincere thanks to our Head of Department Mr. Tarsem Singh
for encouraging and providing all sorts of facilities needed for the file completion.

I take this opportunity to thank all our lecturers who have directly or indirectly
helped my work.

2
Certificate

This is to certify that the entitled Prolog language has been submitted for the
fulfillment of the requirement of the degree of Master of Science in Information
Technology of Panjab University, Chandigarh. This project is bona fide work of
Bhavneet kaur and no part of it has been submitted for any other Degree.

Head of Department : Guide:

Mr. Tarsem Singh Prof. Bohar Singh

Department of Computer Science

3
 Table of contents:

Serial No. Particulars Page No.

1. Introduction to prolog 5

2. Features of prolog 6-10

3. Data types of prolog 10-11

4. Programs of prolog 12-21

5. Project on towers of Hanoi 22-25

6. References 26

4
 Introduction of Prolog :

Prolog is a computer programming language that is used for solving problems that
involves objects and the relationships between objects.

We use prolog when we wish the computer to solve problems that can be
expressed in the form of objects and their relationships. For example, when we say
“john owns the book”, we are declaring that a relationship, ownership exists
between one object “john” and another individual object “the book”. Furthermore,
the relationship has a specific order : john owns the book, but the book doesn‟t
own john! When we ask the question “Does john own the book?”, we are trying to
find out about a relationship.

Computer programming in prolog consists of:

a. Declaring some facts about objects and their relationships,


b. Defining some rules about objects and their relationships, and
c. Asking questions about objects and their relationships.

Prolog is a conversational language,which means that you and the computer carry
out a kind of conversation. Assume that you are seated at a computer terminal have
asked to use prolog. The computer terminal you use a keyboard and a display. You
use the keyboard to type characters into the computer, and the computer uses the
display to type results back to you. Prolog will wait for you to type in the facts and
rules that pertain to the problem you want to solve.

5
 Features of Prolog :

1.Facts :

A fact is a predicate expression that makes a declarative statement about the


problem domain. Whenever a variable occurs in a Prolog expression, it is assumed
to be universally quantified. Note that all Prolog sentences must end with a period.

likes(john, susie). /* John likes Susie */

likes(X, susie). /* Everyone likes Susie */

likes(john, Y). /* John likes everybody */

likes(john, Y), likes(Y, john). /* John likes everybody and everybody likes
John */

likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary */

not(likes(john,pizza)). /* John does not like pizza */

likes(john,susie) :- likes(john,mary)./* John likes Susie if John likes Mary.

6
2.Queries :

The Prolog interpreter responds to queries about the facts and rules represented in
its database. The database is assumed to represent what is true about a particular
problem domain. In making a query you are asking Prolog whether it can prove
that your query is true. If so, it answers "yes" and displays any variable bindings
that it made in coming up with the answer. If it fails to prove the query true, it
answers "No".

Whenever you run the Prolog interpreter, it will prompt you with ?-. For example,
suppose our database consists of the following:

location(desk, office).
location(apple, kitchen).
location(flashlight, desk).
location('washing machine', cellar).
location(nani, 'washing machine').
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).

Questions and answers of following database:

?- location(apple, kitchen).

yes

?- location(kitchen, apple).

no

7
3. Variables :

If you want to find out what things that john likes, it is tiresome to ask does john
like books?, Does john like mary ?, and so forth, with prolog giving a yes-or-no
answer each time.
Consider the following database of facts,followed by a question:
likes(john,flowers).
likes(john,mary).
likes(paul,mary).
?-likes(john,X).
The question asks , is there anything that john likes? When asked the
question , prolog will respond:
X=flowers
In this case only variable was X, and it matched the object flower.

4. Conjuction :

Conjunctions are used to query complex questions.

For example,
You can ask Prolog, „tell me the animal that is bigger than rabbit and smaller than
tiger‟

You can query these kind of questions using , operator. , is pronounced as „and‟.

For example, the statement „bigger(X, rabbit), bigger(tiger, X).‟ return the animal
that is bigger than rabbit and smaller than tiger.
animals.pl
bigger(elephant,fox).

8
bigger(elephant,tiger).
bigger(elephant,rabbit).
bigger(elephant,ant).
bigger(tiger,fox).

bigger(tiger,rabbit).

bigger(tiger,ant).

bigger(fox,rabbit).

bigger(fox,ant).

Open Prolog terminal and execute the statement „bigger(X, rabbit), bigger(tiger,
X).‟

2 ?- bigger(X,rabbit), bigger(tiger,X).
X=fox.

You can even combine multiple predicates. For example, below statement return
the animal that is bigger than ant, rabbit and tiger.

3 ?- bigger(X,ant), bigger(X,rabbit), bigger(X,tiger).


X=elephant.

5. Rules :
A rule is a predicate expression that uses logical implication (:-) to describe a
relationship among facts. Thus a Prolog rule takes the form
left_hand_side :- right_hand_side .This sentence is interpreted as: left hand
side if right hand side. The left hand side is restricted

to a single, positive, literal, which means it must consist of a positive atomic


expression. It cannot be negated and it cannot contain logical connectives.

This notation is known as a Horn clause. In Horn clause logic, the left hand side
of the clause is the conclusion, and must be a single positive literal. The right hand
side contains the premises. The Horn clause calculus is equivalent to the first-order
9
predicate calculus.

Examples of valid rules:

friends(X,Y) :- likes(X,Y),likes(Y,X). /* X and Y are friends if they like


each other */
hates(X,Y) :- not(likes(X,Y)). /* X hates Y if X does not like Y. */
enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). /* X and Y are enemies if they
don't like each other */

Examples of invalid rules:

left_of(X,Y) :- right_of(Y,X) /* Missing a period */


likes(X,Y),likes(Y,X) :- friends(X,Y). /* LHS is not a single literal */
not(likes(X,Y)) :- hates(X,Y). /* LHS cannot be negated */

 Data types in Prolog :

Prolog's single data type is the term. Terms are either , atoms, numbers, variables
and compound term.

 Atom :
An atom is a general-purpose name with no inherent meaning.
Examples of atoms include x , red , 'Taco' , and „some atom‟.

 Numbers:
Numbers can be floats or integers. ISO standard compatible Prolog
systems can check the Prolog flag "bounded". Most of the major
Prolog systems support arbitrary length integer numbers.

 Variables :
10
Variables are denoted by a string consisting of letters, numbers and
underscore characters, and beginning with an upper-case letter or
underscore. Variables closely resemble variables in logic in that they
are placeholders for arbitrary terms.

 Compound term :
A compound term is composed of an atom called a "functor" and a
number of "arguments", which are again terms. Compound terms are
ordinarily written as a functor followed by a comma-separated list of
argument terms, which is contained in parentheses. The number of
arguments is called the term's arity. An atom can be regarded as a
compound term with arity zero. An examples of compound terms
is person_friends ( zelda, [tom,jim] ) .
Special cases of compound terms:

List : A List is an ordered collection of terms. It is denoted by square


brackets with the terms separated by commas or in the case of the empty list, [] .
For example, [1,2,3] or [red,green,blue] .

String : A sequence of characters surrounded by quotes is equivalent to


either a list of (numeric) character codes, a list of characters (atoms of length 1), or
an atom depending on the value of the Prolog flag double_quotes . For
example, "to be, or not to be" .

11
 Programs in Prolog:

1.Program for finding brother sister.

male(albert).
male(edward).
female(alice).
female(victoria).
parents(edward,victoria,albert).
parents(alice,victoria,albert).
sister_of(X,Y):-
female(X),parents(X,F,M),parents(Y,F,M).

12
2. Program finding thief.

likes(mary,food).
likes(mary,wine).
likes(john,X):-
likes(X,wine).
maysteal(X,Y):- thief(X),likes(X,Y),thief(john).
likes(mary,food).
likes(mary,wine).
likes(john,X).
likes(X,wine).
maysteal(X,Y):- thief(X),likes(X,Y).

13
3.Program for finding possible pair.

possible_pairs(X,Y):-
boy(X),girl(Y).
boy(john).
boy(duke).
boy(bert).
boy(charles).
girl(gri).
girl(erm).
girl(bru).

14
4. Program for finding parents.

father(jack,susan).
father(jack,ray).
father(david,liza).
father(david,john).
father(john,peter).
father(john,mary).
mother(karen,susan).
mother(karen,ray).
mother(amy,liza).
mother(amy,john).
mother(susan,peter).
mother(susan,mary).
parent(X,Y):-
father(X,Y).
parent(X,Y):-
mother(X,Y).
mama(X,Y):-
mother(X,Z),father(Z,Y).
gunggung(X,Y):-
father(X,Z),mother(Z,Y).
yeye(X,Y):-
father(X,Z),father(Z,Y),father(jack,susan).

15
16
5. Program for finding reigns.

reigns(rhodri,844,878).
reigns(anarawd,878,916).
reigns(hywel_dda,916,950).
reigns(lago_ap_idwal,950,979).
reigns(hawel_ap_ieuaf,979,985).
reigns(cadwallon,985,986).
reigns(maredudd,986,999).
prince(X,Y):-
reigns(X,A,B),
Y>=A,Y=<B.

17
6. Program for finding area.

pop(usa,203).
pop(india,548).
pop(china,800).
pop(brazil,108).
area(usa,3).
area(india,1).
area(china,4).
area(brazil,3).
density(X,Y):- pop(X,P), area(X,A), Y is P/A.

18
7.Program of fail predicate.

a(X):-
b(X),c(X),fail.
a(X):-
d(X).
b(1).
b(4).
c(1).
c(3).
d(4).

19
8. Program of cut predicate.

a(X):-
b(X),!,c(X).
a(X):-
d(X).
b(1).
b(4).
c(1).
c(3).
d(4).

20
9. Program for finding sum of number.

sum_to(N,1):-
N=<1,!.
sum_to(N,R):-
N1 is N-1,sum_to(N1,R1),R is R1+N.

21
 Project on Tower of Hanoi

Introduction: The Tower of Hanoi (also called


the Tower of Brahma or Lucas' Tower and sometimes pluralized) is a
mathematical game or puzzle. It consists of three rods and a number of disks of
different sizes, which can slide onto any rod. The puzzle starts with the disks in a
neat stack in ascending order of size on one rod, the smallest at the top, thus
making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the
following simple rules:

1. Only one disk can be moved at a time.


2. Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack or on an empty rod.
3. No larger disk may be placed on top of a smaller disk.
With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves
required to solve a Tower of Hanoi puzzle is 2n − 1, where n is the number of
disks.
The prolog program that implements this strategy is define as follows:
We define a predicate Hanoi having one argument, such that Hanoi(N) means to
printout the sequence of moves when N discs are on source pole. On the two move
clauses, the first one is the boundary condition as described above, and the second
clause implement the recursive cases. The predicate move has four arguments. The
first argument is the number of discs to be moved. The other three represent the
poles which are the source, destination and for moving the discs. The predicate
inform uses write to printout the names of the poles that are involved in moving a
disc.

22
Image illustration for 3 disks:

Towers of Hanoi

23
 Program for Tower of Hanoi

hanoi(N):-move(N,left,center,right).
move(0,_,_,_):-!.
move(N,A,B,C):-
M is N-1,
move(M,A,C,B),inform(A,B),move(M,C,B,A).
inform(X,Y):-
write([move,a,disc,from,the,X,pole,to,the,Y,pole]),nl.

24
25
References:

- http://www.gprolog.org/manual/gprolog.html
- Bratko,I.,Prolog Programming for Artificial Intelligence
(3rd edition),2001.
- Artificial Intelligence(1991)
Elaine Rich and Kevin Knight,Second Edition, Tata McGraw Hill.

26

You might also like