Documentos de Académico
Documentos de Profesional
Documentos de Cultura
INDEX
1 Introduction to Prolog. 1
EXPERIMENT NO. 1
Prolog, which stands for PROgramming in LOGic, is the most widely available language in the logic
programming paradigm. Logic and therefore Prolog is based the mathematical notions of relations and
logical inference. Prolog is a declarative language meaning that rather than describing how to compute
a solution, a program consists of a data base of facts and logical relationships (rules) which describe the
relationships which hold for the given application. Rather than running a program to obtain a solution,
the user asks a question. When asked a question, the run time system searches through the data base of
facts and rules to determine (by logical deduction) the answer.
Among the features of Prolog are `logical variables' meaning that they behave like mathematical
variables, a powerful pattern-matching facility (unification), a backtracking strategy to search for
proofs, uniform data structures, and input and output are interchangeable.
Often there will be more than one way to deduce the answer or there will be more than one solution, in
such cases the run time system may be asked find other solutions, backtracking to generate alternative
solutions. Prolog is a weakly typed language with dynamic type checking and static scope rules.
Prolog is used in artificial intelligence applications such as natural language interfaces, automated
reasoning systems and expert systems. Expert systems usually consist of a data base of facts and rules
and an inference engine, the run time system of Prolog provides much of the services of an inference
engine.
Horn Clauses
Consider .
The statement Q will be true if all statements P1, P2, … Pn are true.
In disjunctive normal form the implication will be represented as:
Expressions of this type are called Horn clauses. All Prolog statements are instances of Horn clauses.
The inference in Prolog is based on the modus ponens syllogism
(1) If P(x) then Q(x)
(2) P(a)
(3) Therefore Q(a)
Example:
If human(X) then mortal(X).
human(socrates)
Therefore: mortal(socrates)
A Prolog program consists of logical expressions of type (1) and (2), represented as Prolog clauses
(discussed below).
Building blocks
A Prolog program consists of clauses.
A clause is a term.
Term: constant | variable | structure
Types of clauses:
Rules - if-then logical expressions, Horn clauses.
Facts - predicates with 0 or more arguments.
Rules: structure with functor ' :- ' and two arguments: head and body.
<rule> ::= <rule_head >:- <rule_body>
<rule_head> ::= structure
<rule_body>::= sequence of structures, separated by commas or ;
Facts: rules with an empty body.
Example:
divisible_by_two(X):- even(X).
This is equivalent to the predicate calculus statement
Semantics:
Here is an informal version of the procedural semantics for the example above:
If we can find a value of X that satisfies the goal even(X) then we have also found a number
that satisfies the goal divisible_by_two(X).
The declarative semantics is:
If an integer X is even then X is divisible by two.
Note that there is an implicit universal quantification here. That is, for all objects those that are
even are also divisible by two.
Another example: divisible_by_six(X):- divisible_by_two(X), divisible_by_three(X).
Thus each rule
Q:- P1, P2, …., Pn
corresponds to an implication in predicate logic of the type:
The head of the rule (Q) is the predicate in the right part of the implication.
The body of the rule (P1, P2, …., Pn) contains the predicates in the left part of the implication.
The left part is a conjunction.
Conjunction of predicates is represented as a sequence of structures, separated by commas.
Disjunction in Prolog:
a. using ; to separate structures
b. using separate clauses
Example:
a. siblings(X,Y):- brother(X,Y) ; sister(X,Y).
b. siblings(X,Y):-brother(X,Y).
siblings(X,Y):- sister(X,Y).
For example, in the definition of the predicate "brother(X,Y)" we would not like to have same values
for X and Y, and we write:
brother(X,Y):- male(X), parent(Z,X), parent(Z,Y), not X = Y.
We may negate any predicate, as in:
odd(X):- not even(X).
Scope of Variables
The scope rule for Prolog is that two uses of an identical name for a logical variable only refer to the
same object if the uses are within a single clause. Therefore in
happy(X):-healthy(X).
wise(X):- old(X).
the two references to X in the first clause do not refer to the same object as the references to X in the
second clause.
EXPERIMENT NO. 2
Clauses
likes(ram, mango).
girl(seema).
red(rose).
likes(bill, cindy).
owns(john, gold).
Goal
1?- likes(ram, mango).
Yes
2 ?- likes(bill, cindy).
Yes
3 ?- likes(ram,What).
What = mango ;
No
EXPERIMENT NO. 3
Rules
We said earlier a predicate is defined by clauses, which may be facts or rules. A rule is no more than a
stored query. Its syntax is
head :- body.
where
head a predicate definition (just like a fact) :- the neck symbol, sometimes read as "if" body
one or more goals (a query)
Production rules:
Parse tree:
Clauses:
father(dashrath, ram).
father(ram, luv).
son(kush, ram).
wife(koushaliya, dashrath).
mother(X,Y):-wife(X,Z),father(Z,Y).
grandfather(X,Y):-father(X,Z),father(Z,Y).
grandfather(X,Y):-father(X,Z),son(Y,Z).
Queries:
?- father(Who,ram).
Who= Dashrath.
?- mother(Who,ram).
Who= Koushaliya.
?- grandfather(dashrath,Who).
Who= luv.
Who= Kush.
EXPERIMENT NO. 4
Lists
Lists are powerful data structures for holding and manipulating groups of things.
In Prolog, a list is simply a collection of terms. The terms can be any Prolog data types, including
structures and other lists. Syntactically, a list is denoted by square brackets with the terms separated by
commas. For example, a list of things in the kitchen is represented as
[apple, broccoli, refrigerator]
Rather than having separate location predicates for each thing, we can have one location predicate per
container, with a list of things in the container.
loc_list([apple, broccoli, crackers], kitchen).
loc_list([desk, computer], office).
loc_list([flashlight, envelope], desk).
loc_list([stamp, key], envelope).
loc_list(['washing machine'], cellar).
loc_list([nani], 'washing machine').
The special notation for list structures. [X | Y]
There is a special list, called the empty list, which is represented by a set of empty brackets ([]). It is
also referred to as nil. It can describe the lack of contents of a place or thing.
loc_list([], hall)
Unification works on lists just as it works on other data structures. With what we now know about lists
we can ask
?- loc_list(X, kitchen).
X = [apple, broccoli, crackers]
Production rule:
Clauses:
length([ ] , 0).
length([_ | T], N):- length(T,M), N=M+1.
Queries:
?- length([1,2,3,4,5,6,7], A).
A= 7
?- length([a,b,c,d,e,f,g,h,i], B).
B= 9
EXPERIMENT NO. 5
AIM: Write a program to find union and intersection of two given sets represented as lists.
Clauses:
member(X,[_|R) :- member(X,R).
member(X,[X|_]).
union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W).
union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W).
union([],Z,Z).
intersection([X|Y],M,[X|Z]) :- member(X,M), intersection(Y,M,Z).
intersection([X|Y],M,Z) :- \+ member(X,M), intersection(Y,M,Z).
intersection([],M,[]).
Queries:
?- union([1,2,3,4],[1,a,b,4],A).
A = [2,3,1,a,b,4]
?- intersection([1,2,3,4],[1,a,b,4],B).
B = [1,4]
EXPERIMENT NO. 6
AIM: Write a program to delete the first occurrence and also all occurrences of a particular
element in a given list.
Clauses:
delete(X,[],[]).
delete(X,[X|L],M):-delete(X,L,M).
delete(X,[Y|L],[Y|M]):-not(X=Y),delete(X,L,M).
Queries:
?delete(a,[a,b,a,c],M).
M=[b,c];
M=[b,a,c];
M=[a,b,a,c];
No
EXPERIMENT NO. 7
Facts are:
Cat likes fish.
Tuna is fish.
Tom is cat.
Production Rules:
Parse tree:
Clauses:
likesToEat(X,Y) :- cat(X), fish(Y).
cat(tom).
fish(tuna).
Queries:
?- likesToEat(X, Y), X = tom.
Y = tuna
?- likesToEat(tom, What)
What = tuna
?- likesToEat(Who, tuna)
Who = tom
EXPERIMENT NO. 8
Imagine a room containing a monkey, chair and some bananas. That have been hanged from the center
of ceiling. If the monkey is clever enough he can reach the bananas by placing the chair directly below
the bananas and climb on the chair. The problem is to prove the monkey can reach the bananas.
Clauses:
% initial state: Monkey is at door,
% Monkey is on floor,
% Box is at window,
% Monkey doesn't have banana.
%
% prolog structure: structName(val1, val2, ... )
% state(Monkey location in the room, Monkey onbox/onfloor, box location, has/hasnot banana)
% legal actions
add(X,L,[X|L]).
Queries:
% ?- canget(state(atdoor, onfloor, atwindow, hasnot), Plan).
% Plan = [walk(atdoor, atwindow), push(atwindow, middle), climb, grasp]
% Yes
EXPERIMENT NO. 9
Clauses:
male(james1).
male(charles1).
male(charles2).
male(james2).
male(george1).
female(catherine).
female(elizabeth).
female(sophia).
parent(charles1, james1).
parent(elizabeth, james1).
parent(charles2, charles1).
parent(catherine, charles1).
parent(james2, charles1).
parent(sophia, elizabeth).
parent(george1, sophia).
Queries:
1 ?- parent(charles1, george1).
No
2 ?- parent(charles1,X).
X = james1 ;
No
3 ?- parent(X,charles1).
X = charles2 ;
X = catherine ;
X = james2 ;
No
EXPERIMENT NO. 10
A
C
B
Clauses
on(X,Y).
on(a,b).
on_table(b).
on_table(c).
on_table(X).
clear(X).
clear(a).
clear(c).
holding(X).
arm(empty).
unstuck(X,Y):-arm(empty),clear(X),on(X,Y).
stack(X,Y):- holding(X),clear(Y).
pickup(X):-arm(empty),clear(X),on(X).
putdown(X):-holding(X).
Queries:
?- unstuck(a,b).
yes
?- unstuck(b,a).
No
EXPERIMENT NO. 11
This object of this famous puzzle is to move N disks from the left peg to the right peg using the center
peg as an auxiliary holding peg. At no time can a larger disk be placed upon a smaller disk. The
following diagram depicts the starting setup for N=3 disks.
Production Rules
hanoi(N)move(N,left,middle,right).
move(1,A,_,C)inform(A,C),fail.
move(N,A,B,C)N1=N-1,move(N1,A,C,B),inform(A,C),move(N1,B,A,C).
Diagram
Clauses
% move(N,X,Y,Z) - move N disks from peg X to peg Y, with peg Z being the
% auxilliary peg
%
% Strategy:
% Base Case: One disc - To transfer a stack consisting of 1 disc from
% peg X to peg Y, simply move that disc from X to Y
% Recursive Case: To transfer n discs from X to Y, do the following:
Transfer the first n-1 discs to some other peg X
Move the last disc on X to Y
Transfer the n-1 discs from X to peg Y
move(1,X,Y,_) :-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Queries:
?- move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
yes