Вы находитесь на странице: 1из 29

PROGRAM 1

WHAT IS A PROLOG PROGRAM?

Prolog is used for solving problems that involve objects and the relationships
between objects.
1. A program consists of a database containing one or more facts and zero or more
rules (next week).
2. A fact is a relationship among a collection of objects. A fact is a one-line
statement that ends with a full-stop.
PROLOG CLAUSES….

Any factual expression in Prolog is called a clause.


There are two types of factual expressions: facts and rules
There are three categories of statements in Prolog :
1. Facts: Those are true statements that form the basis for the knowledge base.
2. Rules: Similar to functions in procedural programming (C++, Java…) and has
the form of if/then.
3. Queries: Questions that are passed to the interpreter to access the knowledge
base and start the program.
FACTS….
Syntax rules:
1. The names of all relationships and objects must begin with a lower-case letter.
For example: likes, john, Rachel.
2. The relationship is written first, and the objects are written separated by
commas, and the objects are enclosed by a pair of round brackets.
3. The character ‘.’ must come at the end of each fact.

Turbo Prolog Program:

A Turbo Prolog program consists of two or more sections.


1. Clauses Section
• The main body of the Prolog program.
• Contains the clauses that define the program – facts and rules.
2. Predicates Section
• Predicates (relations) used in the clauses section are defined.

NITIN Page 1
• Each relation used in the clauses of the clauses section must have a
corresponding predicate definition in the predicates section. Except for the built-
in predicates of Turbo Prolog.

A Turbo Prolog may also have a domains section. In this section the
programmer can define the type of each object.
Examples:
Clauses Section – likes (tom, anna).
Predicates Section – likes (boy, girl)
Domains Section – boy, girl = symbol
It is possible to omit the domains section by entering the data types of objects in
the predicates section.
Likes (symbol, symbol)
However, this might make the program harder to read especially if the predicate
associates many objects.

THE STRUCTURE OF A TURBO PROLOG PROGRAM:


EXAMPLE:
domains
person, activity = symbol
predicates
likes(person, activity)
clauses
likes(ellen, tennis).
likes(john, football).
likes(tom, baseball).
likes(eric, swimming).
likes(mark, tennis).
likes(bill, X) if likes(tom, X).

The clauses section contains a collection of facts and rules. The facts
likes(ellen, tennis).
likes(john, football).
likes(tom, baseball).
likes(eric, swimming).
likes(mark, tennis).

correspond to these statements in English:


ellen likes tennis.
john likes football.
tom likes baseball.
NITIN Page 2
eric likes swimming.
mark likes tennis.

To use Prolog to discover if bill likes baseball, we can execute the above Prolog
program with
likes(bill, baseball).
as our goal. When attempting to satisfy this goal, the Prolog system will use the
rule
likes(bill, X) if likes(tom, X).

In ordinary English, this rule corresponds to:


bill likes X if tom likes X.

Type the above program into your computer and Run it. When the system
responds in the dialog window,
Goal :_
enter
likes(bill, baseball).
Turbo Prolog replies
True
Goal :_
in the dialog window and waits for you to give another goal. Turbo Prolog
has combined the rule
likes(bill, X) if likes(tom, X).
with the fact
likes(tom, baseball)
to decide that
likes(bill, baseball)
is true.
Now enter the new goal
likes(bill, tennis).
The system replies:
False
Goal :_
since there is neither a fact that says bill likes tennis nor can this be deduced using
the rule and the available facts. Of course it may be that bill absolutely adores
tennis in real life, but Turbo Prolog's response is based only upon the facts and
the rules you have given it in the program.

NITIN Page 3
PROGRAM 2

PROGRAM TO ADD TWO NUMBERS.


predicates
add
clauses
add:-write("input first number"),
readint(X),
write("input second number"),
readint(Y),
Z=X+Y,
write("output=",Z).

NITIN Page 4
OUTPUT:
Goal: add
input first number5
input second number8
output=13Yes

NITIN Page 5
PROGRAM 3
TO CALCULATE THE FACTORIAL OF A NUMBER

domains
A,B,Y,Z=Integer
predicates
fact(integer,integer)
go
clauses
go:-
write("enter the no."),nl,
readint(A),
B=1,
fact(A,B).
fact(A,B):-
A<>1,
Y=A-1,
Z=A*B,
fact(Y,Z).
fact(1,B):-
write("factorial is",B).

NITIN Page 6
OUTPUT:
Goal:go.
enter the no.
6
factorial is 720yes

NITIN Page 7
PROGRAM 4
PROGRAM TO DEMONSTRATE FAMILY RELATIONSHIP.
predicates
parent(symbol,symbol)
child(symbol,symbol)
mother(symbol,symbol)
brother(symbol,symbol)
sister(symbol,symbol)
grandparent(symbol,symbol)
male(symbol)
female(symbol)
clauses
parent(a,b).
sister(a,c).
male(a).
female(b).
child(X,Y):-parent(Y,X).
mother(X,Y):-female(X),parent(X,Y).
grandparent(X,Y):-parent(X,Z),parent(Z,Y).
brother(X,Y):-male(X),parent(V,X),parent(V,Y).

NITIN Page 8
OUTPUT:
Goal: child(X,h)
No solution
Goal: female(b)
Yes
Goal: male(a)
Yes
Goal: grandparent(a,X)
No solution
Goal: grandparent(a,d)
No

NITIN Page 9
PROGRAM 5
PROGRAM TO CATEGORISE ANIMAL CHARACTERISTICS.
predicates
small(symbol)
large(symbol)
color(symbol,symbol)
clauses
small(rat).
small(cat).
large(lion).
color(dog,black).
color(rabbit,white).
color(X,dark):-
color(X,black);color(X,brown).

NITIN Page 10
OUTPUT:
Goal: small(X)
X=rat
X=cat
2 Solutions
Goal: large(lion)
Yes
Goal: color(cat,brown)
No
Goal: color(rabbit,white)
Yes

NITIN Page 11
PROGRAM 6
PROLOG PROGRAM OF MEDICAL DIAGNOSIS SYSTEM OF
CHILDHOOD DISEASES.
domains
disease,indication=symbol
predicates
symptom(disease,indication)
clauses
symptom(chicken_pox,high_fever).
symptom(chicken_pox,chills).
symptom(flu,running_nose).
symptom(flu,chills).
symptom(cold,body_ache).
symptom(cold,high_fever).
symptom(cold,running_nose).
symptom(flu,moderate_cough).

NITIN Page 12
OUTPUT:
Goal:symptom(flu,chills)yes
Goal:symptom(cold,chills)no

NITIN Page 13
PROGRAM: 7
WRITE A PROGRAM OF BREADTH FIRST SEARCH
domains
X,H,N,ND=symbol
P,L,T,Z,Z1,L1,L2,L3,PS,NP,ST,SOL=symbol*
predicates
solve(L,L)
member(X,L)
extend(L,L)
conc(X,L,L)
breadthfirst(L,L)
goal(X)
clauses
solve(start,solution):- /*solution is a state from start to a goal*/
breadthfirst([[start]],solution).
breadthfirst([[node|path]|_],[node|path]):- /*solution is an extension to a goal*/
/*of one of path*/
goal(node).
breadthfirst([path|paths],solution):-
extend(path,newpaths),
conc(paths,newpaths,path1),
breadthfirst(path1,solution).
extend([node|path],newpaths):-
bagof([newnode,node|path],(s(node,
newnode),notmember(newnode,[node|path])),newpaths),!.
extend(path,[]).
conc([],L,L).
NITIN Page 14
conc([X|L1],L2,[X|L3]):-
conc(L1,L2,L3).
member(X,[X|T]).
member(X,[H|T):-
member(X,T).

NITIN Page 15
OUTPUT:
Goal: solve([a,e],S)
L=[“a”,”b”,”c”,”d”,”e”]
Goal: solve([a,h],S)
L=[“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”]

NITIN Page 16
PROGRAM:8
WRITE A PROGRAM OF DEPTH FIRST SEARCH
domains
X=symbol
Y=symbol*
predicates
child(X,X)
childnode(X,X,Y)
path(X,X,Y)
clauses
child(a,b). /*b is child of a*/
child(a,c). /*c is child of a*/
child(a,d). /*d is child of a*/
child(b,e). /*b is child of b*/
child(b,f). /*f is child of b*/
child(c,g). /*g is child of c*/
path(A,G,[A|Z]):- /*to find the path from root to leaf*/
childnode(A,G,Z).
childnode(A,G,[G]):- /*to determine whether a node is child of other*/
child(A,G).
childnode(A,G,[X|L]):-
child(A,X),
childnode(X,G,L).

NITIN Page 17
OUTPUT:
Goal:-path(a,e,L).
L=[“a”,”b”,”e”]

NITIN Page 18
PROGRAM: 9
PROGRAM TO SOLVE THE 4-3 GALLON WATER JUG PROBLEM.
database

visited_state(integer,integer)

predicates

state(integer,integer)

clauses

state(2,0).

state(X,Y):- X < 4,

not(visited_state(4,Y)),

assert(visited_state(X,Y)),

write("Fill the 4-Gallon Jug: (",X,",",Y,") --> (", 4,",",Y,")\n"),

state(4,Y).

state(X,Y):- Y < 3,

not(visited_state(X,3)),

assert(visited_state(X,Y)),

write("Fill the 3-Gallon Jug: (", X,",",Y,") --> (", X,",",3,")\n"),

state(X,3).

state(X,Y):- X > 0,

not(visited_state(0,Y)),

assert(visited_state(X,Y)),

write("Empty the 4-Gallon jug on ground: (", X,",",Y,") --> (", 0,",",Y,")\n"),

state(0,Y).

state(X,Y):- Y > 0,

not(visited_state(X,0)),
NITIN Page 19
assert(visited_state(X,0)),

write("Empty the 3-Gallon jug on ground: (", X,",",Y,") --> (", X,",",0,")\n"),

state(X,0).

state(X,Y):- X + Y >= 4,

Y > 0,

NEW_Y = Y - (4 - X),

not(visited_state(4,NEW_Y)),

assert(visited_state(X,Y)),

write("Pour water from 3-Gallon jug to 4-gallon until it is full: (", X,",",Y,") -->
(", 4,",",NEW_Y,")\n"),

state(4,NEW_Y).

state(X,Y):- X + Y >=3,

X > 0,

NEW_X = X - (3 - Y),

not(visited_state(X,3)),

assert(visited_state(X,Y)),

write("Pour water from 4-Gallon jug to 3-gallon until it is full: (", X,",",Y,") -->
(", NEW_X,",",3,")\n"),

state(NEW_X,3).

state(X,Y):- X + Y <=4,

Y > 0,

NEW_X = X + Y,

not(visited_state(NEW_X,0)),

assert(visited_state(X,Y)),

NITIN Page 20
write("Pour all the water from 3-Gallon jug to 4-gallon: (", X,",",Y,") --> (",
NEW_X,",",0,")\n"),

state(NEW_X,0).

state(X,Y):- X+Y<=3,

X > 0,

NEW_Y = X + Y,

not(visited_state(0,NEW_Y)),

assert(visited_state(X,Y)),

write("Pour all the water from 4-Gallon jug to 3-gallon: (", X,",",Y,") -->

(", 0,",",NEW_Y,")\n"),

state(0,NEW_Y).

state(0,2):- not(visited_state(2,0)),

assert(visited_state(0,2)),

write("Pour 2 gallons from 3-Gallon jug to 4-gallon: (", 0,",",2,") -->

(", 2,",",0,")\n"),

state(2,0).

state(2,Y):- not(visited_state(0,Y)),

assert(visited_state(2,Y)),

write("Empty 2 gallons from 4-Gallon jug on the ground: (", 2,",",Y,") --> (",
0,",",Y,")\n"),

state(0,Y).

NITIN Page 21
OUTPUT:
Goal

makewindow(1,2,3,"4-3 Water Jug Problem",0,0,25,80),

state(0,0).

Output

----------------------------4-3 Water Jug Problem-------------------------

Fill the 4-Gallon Jug: (0,0) --> (4,0)

Fill the 3-Gallon Jug: (4,0) --> (4,3)

Empty the 4-Gallon jug on ground: (4,3) --> (0,3)

Pour all the water from 3-Gallon jug to 4-gallon: (0,3) --> (3,0)

Fill the 3-Gallon Jug: (3,0) --> (3,3)

Pour water from 3-Gallon jug to 4-gallon until it is full: (3,3) --> (4,2)

Empty the 4-Gallon jug on ground: (4,2) --> (0,2)

Pour all the water from 3-Gallon jug to 4-gallon: (0,2) --> (2,0)

NITIN Page 22
PROGRAM:10
PROGRAM TO SOLVE MONKEY BANANA PROBLEM.

domains
X=string

predicates
take(X,X)
move(X,X)
get_on(X,X)
hit(X,X,X)

go clauses
go:-
take("Monkey","Stick"),
move("Monkey","Chair"),
get_on("Monkey","Chair"),
hit("Monkey","Stick","Banana"),
write("The monkey has hit the banana").

go:-
write("The monkey couldn't reach the banana").

take(Animal,Object):-
write("Does the ",Animal," take the ",Object,"?(y/n)"),
readchar(Reply),
Reply='y'.

move(Animal,Object):-
write("Does the ",Animal," move the ",Object,"?(y/n)"),
readchar(Reply),
Reply='y'.

get_on(Animal,Object):-
write("Does the ",Animal," get on ",Object,"?(y/n)"),
readchar(Reply),
Reply='y'.

hit(Animal,Object,Fruit):-
write("Does the ",Animal," hit the ",Fruit,"with the",Object,"?(y/n)"),
readchar(Reply),
Reply='y'.

NITIN Page 23
OUTPUT:
Goal:go
Does the Monkey take the Stick?(y/n)
y
Does the Monkey move the Chair?(y/n)
y
Does the Monkey get on Chair?(y/n)
y
Does the Monkey hit the Banana with the Stick?(y/n)
y
The monkey has hit the banana

Goal:go
Does the Monkey take the Stick?(y/n)
y
Does the Monkey move the Chair?(y/n)
n
The monkey couldn't reach the banana

NITIN Page 24
PROGRAM 11
PROGRAM TO FIND OUT THE MAXIMUM AND MINIMUM VALUES
AMONG THREE NUMBERS.
domains
predicates
max(integer,integer,integer)
min(integer,integer,integer)
maxmin(integer,integer,integer)
clauses
max(A,B,C):-
A>B,
A>C,
write(A).
max(A,B,C):-
A>B,
write(C).
max(_,B,C):-
B>C,
write(B).
max(_,_,C):-
write(C).

min(A,B,C):-
A<B,
A<C,
write(A).
min(A,B,C):-

NITIN Page 25
A<B,
write(C).
min(_,B,C):-
B<C,
write(B).
min(_,_,C):-
write(C).

maxmin(A,B,C):-
max(A,B,C),
min(A,B,C).

NITIN Page 26
OUTPUT:
Goal:max(23,7,20)
23Yes
Goal:min(14,30,3)
3Yes
Goal:maxmin(17,39,5)
39
5Yes

NITIN Page 27
PROGRAM 12
PROGRAM TO FIND WHEATHER THE LENGTH OF A LIST IS EVEN
OR ODD.
domains
x = integer
l = integer*
predicates
length(l,x)
evenlength(l)
oddlength(l)
clauses
length([],0).
length([X|List],Length) :-
length(List,Length1),
Length = Length1 + 1.
evenlength(List) :-
length(List,Length),
Length mod 2 = 0.
oddlength(List) :-
length(List,Length),
Length mod 2 <> 0.

NITIN Page 28
OUTPUT:
For evenlength :
Goal: evenlength([1,2,3,4])
Yes
Goal: evenlength([1,2,3,4,5])
No
For oddlength :
Goal: oddlength([1,2,3,4,5])
Yes
Goal: oddlength([1,2,3,4])
No

NITIN Page 29

Вам также может понравиться