Professional Documents
Culture Documents
S No. Name of Program Page No. Date Signature: Index
S No. Name of Program Page No. Date Signature: Index
Date Signature
Introduction to Prolog,
1. What is Prolog?, 2-3 22-8-2013
Programming Logic.
Write a prolog expression
to solve the company 4
2. 29-8-2013
problem.
Write a prolog expression
3. to solve the game 5 12-9-2013
problem.
Write a prolog expression
4. to solve the problem of 6 19-9-2013
the relations.
Write a prolog expression
5. to solve the factorial 7 26-9-2013
problem.
Write a prolog expression
6. to solve the routing 8 3-10-2013
problem between cities.
INDEX
-1-
INTRODUCTION TO PROLOG
The name Prolog was chosen by Philippe Roussel as an abbreviation for programmation
en logique (French for programming in logic). It was created around 1972 by Alain
Colmerauer with Philippe Roussel, based on Robert Kowalski's procedural interpretation
of Horn clauses. It was motivated in part by the desire to reconcile the use of logic as a
declarative knowledge representation language with the procedural representation of
knowledge that was popular in North America in the late 1960s and early 1970s.
According to Robert Kowalski, the first Prolog system was developed in 1972 by
Colmerauer and Phillipe Roussel. The first implementations of Prolog were interpreters,
however, David H. D. Warren created the Warren Abstract Machine, an early and
influential Prolog compiler which came to define the "Edinburgh Prolog" dialect which
served as the basis for the syntax of most modern implementations.
WHAT IS PROLOG?
Prolog has its roots in first-order logic, a formal logic, and unlike many other
programming languages, Prolog is declarative: the program logic is expressed in terms of
relations, represented as facts and rules. A computation is initiated by running a query
over these relations.
Prolog was one of the first logic programming languages, and remains the most popular
among such languages today, with many free and commercial implementations available.
While initially aimed at natural language processing, the language has since then stretched
far into other areas like theorem proving, expert systems, games, automated answering
systems, ontologies and sophisticated control systems. Modern Prolog environments
support creating graphical user interfaces, as well as administrative and networked
applications.
The Prolog programming language is used to handle matters relating to objects and the
relationships between separate objects. Prolog is not object-oriented programming. Prolog
is actually logic programming and is a system of implementing intelligent program
execution, e.g. artificial intelligence. This involves pattern-directed procedure call, non-
determinism and parallelism. An object in Prolog is different than an object in C++ or
Java. Relationships are represented in the form of rules in Prolog.
Prolog enables the structuring of data. It also provides ways to structure the method of
reaching goals and the accomplishment of goals. A structure (a collection of components),
a variable and a constant are all terms from which Prolog programs are made. Prolog
performs tasks when asked questions by the programmer. The method by which it can
-2-
perform these tasks is dependent upon whether it is storing facts or rules relating to the
question asked by the programmer.
Programming Logic
The Prolog programming language is used to handle matters relating to objects and the
relationships between separate objects. Prolog is not object-oriented programming. Prolog
is actually logic programming and is a system of implementing intelligent program
execution, e.g. artificial intelligence. This involves pattern-directed procedure call, non-
determinism and parallelism. An object in Prolog is different than an object in C++ or
Java. Relationships are represented in the form of rules in Prolog.
Prolog is a first step toward programming logic. Use of the Prolog programming language
involves utilizing questions, variables, facts, rules, conjunctions, recursion and lists.
Prolog is a rules and facts 'storage warehouse.' It enables the answering of questions based
upon those rules and facts that are stored. In Prolog, the names of all objects and
relationships must be typed in beginning with a lowercase letter.
The order must be with relationships entered first. The objects must have commas
separating them and be enclosed by round brackets. Consistency is important when using
the Prolog programming language. The number of objects in relationships in Prolog can be
arbitrary.
-3-
PRACTICAL-1
Write a prologue expression which represents the semantic net for the
following:-
Company XYZ is software development company. 3 departments within
the company are sales, administration, programming. Neeraj is the
manager of the programming. Ramesh and Dolly are the programmers.
She is married to Sonu. Sonu is the editor of TMH. They have three
children and they live in Lucknow. She wears glasses and is 5 ft. tall.
Program:-
domains
name of company, type of company, name of department, person, job, things,
height_of_person, designation, relation, place
predicates
Company(name_of_company, type_of_company)
Departments(name_of_company, mane_of_department)
Designation(person, job)
Parent(person, person)
Designation(person, name_of_company)
Relation(person, person)
Lived in(person, place)
Wears(person, thing)
Tall(person, height_of_person)
clauses
Company(XYZ, Software_development).
Departments(XYZ,[Sales, Administration, Programming]).
Manager(Neeraj, programming).
Programmer(Ramesh, Dolly).
Married(Dolly, Sonu).
Editor(Sonu, TMH).
Parent([Dolly, Sonu],three children).
Lived([Dolly, Sonu], Deokali).
Wears(Dolly, Glasses).
Tall(Dolly, Five Feet).
-4-
PRACTICAL-2
Write a prologue expression which represents the semantic net for the
following:-
There are 6 people and 6 different games. Each person likes one of the
games.
Program:-
domains
person, activity= symbol
predicates
likes(person, activity)
clauses
likes(Ellen, Tennis).
likes(John, Football).
likes(Tom, Baseball).
likes(Mark, Tennis).
likes(Bill, X) if likes(Tom, X).
-5-
PRACTICAL-3
Write a prologue expression which represents the semantic net for the
following:-
There is a family with some male and female members. Each member of
the family is related to every other member of the family. There is father,
mother, sister, brother, uncle and grandfather in the family.
Program:-
domains
person= symbol
predicates
male(person)
female(person)
father(person, person)
mother(person, person)
sister(person, person)
parent(person, person)
brother(person, person)
uncle(person, person)
grandfather(person, person)
clauses
male(Alan).
male(Charles).
male(Bob).
male(Ivan).
female(Beverly).
female(Fay).
female(Marilyn).
female(Sally).
mother(Marilyn, Beverly).
mother(Alan, Sally).
father(Alan, Bob).
father(Beverly, Charles).
father(Fay, Bob).
father(Marilyn, Alan).
-6-
PRACTICAL-4
Write a prologue expression which represents the semantic net for the
following:-
To find the factorial the factorial and the residue of a number. Consider
the number to be N.
Program:-
domains
n, f= real
predicates
factorial(n, f)
clauses
factorial(1, 1).
factorial(N, Res) if
N>0 and
N1= N-1 and
factorial(N1, FacN1) and
Res= N*FacN1.
-7-
PRACTICAL-5
Write a prologue expression which represents the semantic net for the
following:-
There are given a number cities. There is a road from one city to another
The route between the cities is defined and the formula to find the route
is given.
Program:-
domains
town= symbol
distance= integer
predicates
road(town, town, distance)
route(town, town, distance)
clauses
road(Tampa, Houstan, 20).
road(Gordon, Tampa, 30).
road(Houston, Gordon, 10).
road(Houston, Kansas_city, 12).
road(Gordon, Kansas_city, 13).
-8-
PRACTICAL-6
1.(Reverse '(a(bc)(de))).
((de)(bc)a)
2. (great rp 18,151,76)
151
3. (+(/10 5) 50)
52
-9-
PRACTICAL- 7
(defun C-to-F()
(terpri)
(princ "please enter the centigrate temperature")
(setq((read))
(princ" The Fahrenheit temperature is ")
(princ(+(*/95)C)32)
(terpri))
- 10 -
PRACTICAL- 8
Write a function in LISP that reads a natural number n & returns n!:-
: (COND
: ((EQUAL N 0) 1)
: ))))
- 11 -
PRACTICAL- 9
- 12 -
;;; Now we create a new hash table LONGITUDE-INFO to
;;; support the heuristic ordering mechanism.
(let ((longitude-info (make-hash-table :size 20)))
(defun set-longitude (x y)
(setf (gethash x longitude-info) y) )
(defun get-longitude (x)
(gethash x longitude-info) )
)
- 13 -
(set-f-value j val)
(setf open
(insert j
(remove j open)
open
val) ) ) ) )
(dolist (j (list-difference l (append open closed)))
;; open the node J:
(increment-count)
(set-f-value j (setf val (f j)))
(setf open (insert j open val))
(set-predecessor j n) )
; end of loop -------- this is implicitly step8
)))
- 14 -
(defun f (n)
"Return diff. in longitude from goal node."
(longitude-diff n (get-goal)) )
(test)
- 15 -
PRACTICAL- 10
BREADTH-FIRST SEARCH:-
- 17 -
(format t "~s nodes expanded.~%"
(get-count) )
)
(test)
- 18 -
PRACTICAL- 11
DEPTH-FIRST SEARCH:-
- 19 -
(loop
(if (null open)(return 'failure)) ;step2
(setf n (pop open)) ;step3
(push n closed)
(increment-count)
(if (eql n goal-node)
(return (extract-path n)) )
(setf l (successors n)) ;step4
(setf l (list-difference l closed))
(setf open
(append l (list-difference open l)));step5
(dolist (x l)
(set-predecessor x n) )
; end of loop -------- this is implicitly step6
)))
- 20 -
(depth-first-graph-search 'rennes 'avignon) )
(format t "~s nodes expanded.~%"
(get-count))
)
(test)
- 21 -
PRACTICAL- 12
% Constants:
% {floor,chair,bananas,monkey}
%Variables:
%{X,Y,Z}
%Predicates:
%{can-reach(X,Y) ;X can reach Y
%dexterous(X); X is a dexterous animal
% close(X,Y) ; Xis close to Y
% get-on(X,Y) ; X can get on Y
% under(X,Y) ; X is under Y
% tall(X) ;X is tall
%in-room(X); X is in the room
%can-move(X,Y,Z) ; X can move Y near Z
% cam climb (X,Y)} ; X can climb onto Y
% Axioms :
in -room(bananas).
in-room(chair).
in-room(monkey).
dexterous(monkey).
tall(chair).
can-move(monkey, chair, bananas).
can-climb(money, char).
Can-reach(X,Y):-
dexterious(X),close(X,Y).
close(X,Y):-
get-on(X,Y).
under(Y,Z).
tall(Y).
get-on(X,Y):-
can-climb(X,Y).
Under(Y,Z):-
in-room(X),
in-room(Y),
in-room(Z),
can-move(X,Y,Z).
- 22 -
- 23 -