Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 23

S No. Name of Program Page No.

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.

Evaluate the Lisp


7. 9 10-10-2013
expression.

Write a program in LISP


10
to convert centigrate
8. 17-10-2013
temperature to farenhiet.

Write a function in LISP


that reads a natural
9. 11 24-10-2013
number n & returns n!.

BEST FIRST SEARCH


10. 12-15 7-11-2013

11. BREADTH FIRST 16-18 14-11-2013


SEARCH

DEPTH FIRST SEARCH 19-21 21-11-2013


12.

Write prolog program to


solve Monkey Banana 22 28-11-2013
13. Problem.

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 is a general purpose logic programming language associated with artificial


intelligence and computational linguistics.

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.

A database is a collection of facts in Prolog. A question in Prolog is written with a


question mark and then a dash before the question. Prolog provides yes and no answers to
questions. In Prolog, any word beginning with a capital letter is a variable. Prolog requires
consistent interpretation of variables. Through the Prolog programming language, a
programmer can use variables and identify their scopes, ask questions about facts, assert
facts regarding objects, use conjunction, represent relationships and use backtracking all as
part of manipulating data in a database for a variety of purposes.

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

In Prolog, program logic is expressed in terms of relations, and a computation is initiated


by running a query over these relations. Relations and queries are constructed using
Prolog's single data type, the term. Relations are defined by clauses.

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).

parent(X, Y) if mother(X, Y).


parent(X, Y) if father(X, Y).

-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).

route(Town1, Town2, Distance):-


road(Town1, Town2, Distance).
route(Town1, Town2, Distance):-
road(Town1, X, Dist1).
route(X, Town2, Dist2).
Distance= Dist1+Dist2!.

-8-
PRACTICAL-6

Evaluate the following Lisp Expression:-

1.(Reverse '(a(bc)(de))).
((de)(bc)a)

2. (great rp 18,151,76)
151

3. (+(/10 5) 50)
52

-9-
PRACTICAL- 7

Write a program in LISP to convert centigrate temperature to


farenhiet:-

(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!:-

:(DEFINE (FACTORIAL (LAMBDA (N)

: (COND

: ((EQUAL N 0) 1)

: (T (MULT N (FACTORIAL (SUB N 1))))

: ))))

- 11 -
PRACTICAL- 9

BEST FIRST SEARCH:-

;;; Best-First Search (Forward-Looking version).


;;; It uses an evaluation function based on the ordering of cities
;;; with respect to longitude. The method does not take into
;;; consideration the length of each partial path found so far.
;;; Thus it considers only the "remaining distance" to the goal
;;; in deciding how to order nodes on OPEN. Hence it is
;;; "forward-looking".

;;; Here is the adjacency data (identical to that used


;;; in DEPTHFS.CL and BREADTH.CL):

;;; Create the hash table ADJACENCY-INFO to store the French


;;; roads data, and define the functions for entering and
;;; retrieving this info.
;;; The second hash table, PATH-PREDECESSOR-INFO, is used
;;; during the search to allow the eventual tracing of
;;; the path back from the goal to the starting node.
(let ((adjacency-info (make-hash-table :size 20))
(path-predecessor-info (make-hash-table :size 20)) )
(defun set-adj (x y)
(setf (gethash x adjacency-info) y) )
(defun get-adj (x)
(gethash x adjacency-info) )
(defun set-predecessor (x y)
(setf (gethash x path-predecessor-info) y) )
(defun get-predecessor (x)
(gethash x path-predecessor-info) )
)

;;; Establish BEST's list of neighbors as (RENNES), etc.


(set-adj 'best '(rennes))
(set-adj 'rennes '(caen paris best nantes))
(set-adj 'caen '(calais paris rennes))
(set-adj 'calais '(nancy paris caen))
(set-adj 'nancy '(strasbourg dijon paris calais))
(set-adj 'strasbourg '(dijon nancy))
(set-adj 'dijon '(strasbourg lyon paris nancy))
(set-adj 'lyon '(grenoble avignon limoges dijon))
(set-adj 'grenoble '(avignon lyon))
(set-adj 'avignon '(grenoble marseille montpellier lyon))
(set-adj 'marseille '(nice avignon))
(set-adj 'nice '(marseille))
(set-adj 'montpellier '(avignon toulouse))
(set-adj 'toulouse '(montpellier bordeaux limoges))
(set-adj 'bordeaux '(limoges toulouse nantes))
(set-adj 'limoges '(lyon toulouse bordeaux nantes paris))
(set-adj 'nantes '(limoges bordeaux rennes))
(set-adj 'paris '(calais nancy dijon limoges rennes caen))

- 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) )
)

;;; The longitude of each city is stored in tenths of a degree.


;;; We use a local function with a LAMBDA form, since
;;; SET-LONGITUDE takes two arguments but we want a
;;; function that takes one argument for this use with MAPCAR.
(mapcar #'(lambda (pair) (apply #'set-longitude pair))
'((avignon 48)(bordeaux -6)(brest -45)(caen -4)
(calais 18)(dijon 51)(grenoble 57)(limoges 12)
(lyon 48)(marseille 53)(montpellier 36)
(nantes -16)(nancy 62)(nice 73)(paris 23)
(rennes -17)(strasbourg 77)(toulouse 14) ) )

;;; We need one more hash table F-VALUE to


;;; remember the heuristic value at each node visited.
(let ((f-values (make-hash-table :size 20)))
(defun set-f-value (x y)
(setf (gethash x f-values) y) )
(defun get-f-value (x)
(gethash x f-values) )
)

;;; BEST-FIRST-SEARCH is the main searching procedure.


(defun best-first-search (start-node goal-node)
"Performs a best-first search from START-NODE for GOAL-NODE."
(set-goal goal-node)
(let ((open (list start-node)) ;step1
(closed nil)
n l val)
(set-predecessor start-node nil)
(set-f-value start-node (f start-node))
(loop
(if (null open)(return 'failure)) ;step2
(setf n (select-best open)) ;step3
(setf open (remove n open)) ;step4
(push n closed)
(if (eql n (get-goal)) ;step5
(return (extract-path n)) )
(setf l (successors n)) ;step6
(setf l (list-difference l closed))
(dolist (j (intersection l open)) ;step7
(if (< (setf val (f j))
(get-f-value j) )
(progn

- 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
)))

;; The supporting functions:

;;; Use local variable to keep track of the goal.


(let (goal)
(defun set-goal (the-goal) (setf goal the-goal))
(defun get-goal () goal) )

;;; EXTRACT-PATH returns the sequence of cities found.


(defun extract-path (n)
"Returns the path to N."
(cond ((null n) nil)
(t (append (extract-path (get-predecessor n))
(list n) )) ) )

;;; SUCCESSORS retrieves the list of cities adjacent


;;; to N from N's property list.
(defun successors (n)
"Returns the list of nodes adjacent to N."
(get-adj n) )

;;; LIST-DIFFERENCE is like the built-in Lisp function


;;; named SET-DIFFERENCE but it preserves the ordering in LST1"
(defun list-difference (lst1 lst2)
"Returns a list of those elements of LST1 that do not
occur on LST2."
(dolist (elt lst2 lst1)
(setf lst1 (remove elt lst1)) ) )

;;; LONGITUDE-DIFF returns the absolute value of the


;;; difference in longitudes between nodes N1 and N2
;;; in tenths of a degree.
(defun longitude-diff (n1 n2)
"Computes difference in longitudes."
(abs (- (get-longitude n1) (get-longitude n2))) )

;;; F evaluates the difference in longitude between


;;; the current node N and the goal node.

- 14 -
(defun f (n)
"Return diff. in longitude from goal node."
(longitude-diff n (get-goal)) )

;;; SELECT-BEST chooses a node in step 3...


(defun select-best (lst)
"Determines the best node to open next."
(cond ((eql (first lst) (get-goal))(first lst))
(t (better (first lst)(rest lst))) ) )

;;; The helping function BETTER for select-best checks


;;; to see if there is a goal node on LST with FVALUE
;;; as low as that of ELT.
(defun better (elt lst)
"Helping function for SELECT-BEST."
(cond ((null lst) elt)
((< (get-f-value elt)(get-f-value (first lst)))
elt)
((eql (first lst) (get-goal))
(first lst) )
(t (better elt (rest lst))) ) )

;;; INSERT puts NODE onto LST, which is ordered


;;; by FVALUE property, where VAL is the FVALUE
;;; of NODE.
(defun insert (node lst val)
"Puts NODE into its proper place on LST."
(cond ((null lst)(list node))
((< val (get-f-value (first lst)))
(cons node lst))
(t (cons (first lst)(insert node (rest lst) val))) ) )

;;; Use a local variable EXPANSION-COUNT for counting the


;;; number of nodes expanded by the algorithm.
(let (expansion-count)
(defun initialize-count () (setf expansion-count 1))
(defun increment-count () (incf expansion-count))
(defun get-count () expansion-count) )

;;; TEST sets EXPANSION-COUNT to 0 and


;;; begins a search from RENNES to AVIGNON.
(defun test ()
"Tests the function BEST-FIRST-SEARCH."
(initialize-count)
(format t "Best-first-search solution: ~s.~%"
(best-first-search 'rennes 'avignon) )
(format t "~s nodes expanded.~%"
(get-count) )
)

(test)

- 15 -
PRACTICAL- 10

BREADTH-FIRST SEARCH:-

;;; The graph to be searched represents roads in France.

;;; Create a hash table ADJACENCY-INFO to store the French


;;; roads data, and define the functions for entering and
;;; retrieving this info.
;;; A second hash table, PATH-PREDECESSOR-INFO, is used
;;; during the search to allow the eventual tracing of
;;; the path back from the goal to the starting node.
(let ((adjacency-info (make-hash-table :size 20))
(path-predecessor-info (make-hash-table :size 20)) )
(defun set-adj (x y)
(setf (gethash x adjacency-info) y) )
(defun get-adj (x)
(gethash x adjacency-info) )
(defun set-predecessor (x y)
(setf (gethash x path-predecessor-info) y) )
(defun get-predecessor (x)
(gethash x path-predecessor-info) )
)

;;; Establish BREST's list of neighbors as (RENNES), etc.


(set-adj 'brest '(rennes))
(set-adj 'rennes '(caen paris brest nantes))
(set-adj 'caen '(calais paris rennes))
(set-adj 'calais '(nancy paris caen))
(set-adj 'nancy '(strasbourg dijon paris calais))
(set-adj 'strasbourg '(dijon nancy))
(set-adj 'dijon '(strasbourg lyon paris nancy))
(set-adj 'lyon '(grenoble avignon limoges dijon))
(set-adj 'grenoble '(avignon lyon))
(set-adj 'avignon '(grenoble marseille montpellier lyon))
(set-adj 'marseille '(nice avignon))
(set-adj 'nice '(marseille))
(set-adj 'montpellier '(avignon toulouse))
(set-adj 'toulouse '(montpellier bordeaux limoges))
(set-adj 'bordeaux '(limoges toulouse nantes))
(set-adj 'limoges '(lyon toulouse bordeaux nantes paris))
(set-adj 'nantes '(limoges bordeaux rennes))
(set-adj 'paris '(calais nancy dijon limoges rennes caen))

;;; BREADTH-FIRST-SEARCH is the main searching procedure.


(defun breadth-first-search (start-node goal-node)
"Performs a breadth-first search from START-NODE for GOAL-NODE."
(let ((open (list start-node)) ;step1
(closed nil)
n l)
(set-predecessor start-node nil)
(loop
- 16 -
(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 (append open closed)))
(setf open (append open l) ) ;step5
(dolist (x l)
(set-predecessor x n) )

; end of loop -------- this is implicitly step6


)))

;; The supporting functions:

;;; EXTRACT-PATH returns the sequence of cities found.


(defun extract-path (n)
"Returns the path to N."
(cond ((null n) nil)
(t (append (extract-path (get-predecessor n))
(list n) )) ) )

;;; SUCCESSORS retrieves the list of cities adjacent


;;; to N from N's property list.
(defun successors (n)
"Returns a list of the nodes adjacent to N."
(get-adj n) )

;;; LIST-DIFFERENCE is like the built-in Lisp function


;;; named SET-DIFFERENCE but it preserves the ordering in LST1"
(defun list-difference (lst1 lst2)
"Returns a list of those elements of LST1 that do not
occur on LST2."
(dolist (elt lst2 lst1)
(setf lst1 (remove elt lst1)) ) )

;;; Use a local variable EXPANSION-COUNT for counting the


;;; number of nodes expanded by the algorithm.
(let (expansion-count)
(defun initialize-count () (setf expansion-count 0))
(defun increment-count () (incf expansion-count))
(defun get-count () expansion-count) )

;;; TEST sets EXPANSION-COUNT to 0 and


;;; begins a search from RENNES to AVIGNON.
(defun test ()
"Tests the function BREADTH-FIRST-SEARCH."
(initialize-count)
(format t "Breadth-first-search solution: ~s.~%"
(breadth-first-search 'rennes 'avignon) )

- 17 -
(format t "~s nodes expanded.~%"
(get-count) )
)

(test)

- 18 -
PRACTICAL- 11

DEPTH-FIRST SEARCH:-

;;; the graph to be searched represents roads in France.

;;; Create a hash table ADJACENCY-INFO to store the French


;;; roads data, and define the functions for entering and
;;; retrieving this info.
;;; A second hash table, PATH-PREDECESSOR-INFO, is used
;;; during the search to allow the eventual tracing of
;;; the path back from the goal to the starting node.
(let ((adjacency-info (make-hash-table :size 20))
(path-predecessor-info (make-hash-table :size 20)) )
(defun set-adj (x y)
(setf (gethash x adjacency-info) y) )
(defun get-adj (x)
(gethash x adjacency-info) )
(defun set-predecessor (x y)
(setf (gethash x path-predecessor-info) y) )
(defun get-predecessor (x)
(gethash x path-predecessor-info) )
)

;;; Establish BREST's list of neighbors as (RENNES), etc.


(set-adj 'brest '(rennes))
(set-adj 'rennes '(caen paris brest nantes))
(set-adj 'caen '(calais paris rennes))
(set-adj 'calais '(nancy paris caen))
(set-adj 'nancy '(strasbourg dijon paris calais))
(set-adj 'strasbourg '(dijon nancy))
(set-adj 'dijon '(strasbourg lyon paris nancy))
(set-adj 'lyon '(grenoble avignon limoges dijon))
(set-adj 'grenoble '(avignon lyon))
(set-adj 'avignon '(grenoble marseille montpellier lyon))
(set-adj 'marseille '(nice avignon))
(set-adj 'nice '(marseille))
(set-adj 'montpellier '(avignon toulouse))
(set-adj 'toulouse '(montpellier bordeaux limoges))
(set-adj 'bordeaux '(limoges toulouse nantes))
(set-adj 'limoges '(lyon toulouse bordeaux nantes paris))
(set-adj 'nantes '(limoges bordeaux rennes))
(set-adj 'paris '(calais nancy dijon limoges rennes caen))

;;; DEPTH-FIRST-SEARCH is the main searching procedure.


;;; Note that we use Common Lisp macros PUSH and POP to simplify
;;; adding and removing elements at the front of lists.
(defun depth-first-graph-search (start-node goal-node)
"Performs a depth-first search from START-NODE for GOAL-NODE."
(let ((open (list start-node)) ;step1
(closed nil)
n l)

- 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
)))

;; The supporting functions:

;;; EXTRACT-PATH returns the sequence of cities found.


(defun extract-path (n)
"Returns the path leading to N."
(cond ((null n) nil)
(t (append (extract-path (get-predecessor n))
(list n) )) ) )

;;; SUCCESSORS retrieves the list of cities adjacent


;;; to N from N's property list.
(defun successors (n)
"Returns a list of nodes adjacent to N."
(get-adj n) )

;;; LIST-DIFFERENCE is like the built-in Lisp function


;;; named SET-DIFFERENCE but it preserves the ordering in LST1"
(defun list-difference (lst1 lst2)
"Returns a list of those elements of LST1 that do not
occur on LST2."
(dolist (elt lst2 lst1)
(setf lst1 (remove elt lst1)) ) )

;;; Use a local variable EXPANSION-COUNT for counting


;;; the number of nodes expanded by the algorithm.
(let (expansion-count)
(defun initialize-count () (setf expansion-count 0))
(defun increment-count () (incf expansion-count))
(defun get-count () expansion-count) )

;;; TEST sets the count of nodes expanded to 0 and


;;; begins a search from RENNES to AVIGNON.
(defun test ()
"Tests the function DEPTH-FIRST-SEARCH."
(initialize-count)
(format t "Depth-first-search solution: ~s.~%"

- 20 -
(depth-first-graph-search 'rennes 'avignon) )
(format t "~s nodes expanded.~%"
(get-count))
)

(test)

- 21 -
PRACTICAL- 12

Write PROLOG program to solve Monkey Banana Problem:-

% 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 -

You might also like