Professional Documents
Culture Documents
A1 PDF
A1 PDF
of
Prolog Language
of
Roll No:-3806
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.
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.
3
Table of contents:
1. Introduction to prolog 5
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.
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 :
likes(john, Y), likes(Y, john). /* John likes everybody and everybody likes
John */
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).
?- 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 :
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.
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
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.
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:
11
Programs in Prolog:
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
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