25
Ques.1. /* Facts--- */ ako(chair, furniture). ako(chair, seat). isa(your_chair, chair). isa(you, person). made_of(your_chair, wood). colour(wood, brown). belongs_to(your_chair, you). /* Queries-- 1. What is the colour of your chair? 2. Determine whether your chair is a seat. 3. Determine whether your chair is a furniture. 4. Determine whether your chair belongs to a person.

31-40 Prolog Program

Embed Size (px)

DESCRIPTION

prolog

Citation preview

Page 1: 31-40 Prolog Program

Ques.1.

/* Facts--- */

ako(chair, furniture).

ako(chair, seat).

isa(your_chair, chair).

isa(you, person).

made_of(your_chair, wood).

colour(wood, brown).

belongs_to(your_chair, you).

/*

Queries--

1. What is the colour of your chair?

2. Determine whether your chair is a seat.

3. Determine whether your chair is a furniture.

4. Determine whether your chair belongs to a person.

Solutions---

Page 2: 31-40 Prolog Program

1. What is the colour of your chair?

Ans-- made_of(your_chair,X),colour(X,Y).

output-- X = wood

Y = brown

yes

2. Determine whether your chair is a seat.

3. Determine whether your chair is a furniture.

Ans-- isa(your_chair, X),ako(X,Y).

output-- X = chair

Y = furniture ? ;

X = chair

Y = seat

yes

4. Determine whether your chair belongs to a person.

Ans-- belongs_to(X,you),isa(you,Y).

output-- X = your_chair

Y = person

Page 3: 31-40 Prolog Program

yes

*/

Page 4: 31-40 Prolog Program

Ques.2-Verifies if 3 numbers can be the sides of a triangle

Facts and Rules:-

start:- write('input a= '),read(A),

write('input b= '),read(B),

write('input c= '),read(C),

A >= 0,B>= 0,C >= 0, /* must be positive */

A < B+C,B< C+A,C < A+B,

write('These numbers are the sides of a triangle.').

Queries :-

1.

| ?- start.

input a= 3.

input b= 4.

input c= 5.

These numbers are the edges of a triangle.

(15 ms) yes

2.

| ?- start.

input a= 18.

Page 5: 31-40 Prolog Program

input b= 34.

input c= 23.

These numbers are the edges of a triangle.

yes

3.

| ?- start.

input a= 12.

input b= 13.

input c= 14.

These numbers are the edges of a triangle.

yes

Ques3.-Problem of likes and not likes.

Facts:-

likes(john, susie). /* John likes Susie */

likes(X, susie). /* Everyone likes Susie */

likes(john, Y). /* John likes everybody */

likes(john, Y), likes(Y, john). /* John likes everybody and everybody likes John */

likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary */

Page 6: 31-40 Prolog Program

not(likes(john,pizza)). /* John does not like pizza */

Rules :-

likes(john,susie) :- likes(john,mary). /* John likes Susie if John likes Mary*/

friends(X,Y) :- likes(X,Y),likes(Y,X). /* X and Y are friends if they like each other */

hates(X,Y) :- not(likes(X,Y)). /* X hates Y if X does not like Y*/

enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). /* X and Y are enemies if they don't like each other */

Queries(with output):-

1.

| ?- hates(john,Y).

Y = pizza

yes

2.?- friends(john,susie).

no

Page 7: 31-40 Prolog Program

3.

| ?- enemies(john,marry).

no

4.

| ?- friends(john,marry).

no

5.

| ?- likes(X,Y).

X = john

Y = susie ?

(15 ms) yes

Page 8: 31-40 Prolog Program

Ques4.

/* Facts---- */

animal(mammal, tiger, carnivore, stripes).

animal(mammal, lion, carnivore, mane).

animal(mammal, hyena, carnivore, ugly).

animal(mammal, zebra, herbivore, stripes).

animal(bird, eagle, carnivore, large).

animal(bird, sparrow, scavenger, small).

animal(reptile, snake, carnivore, long).

animal(reptile, lizard, scavenger, small).

/*Rules*/

animal_name(X):-animal(A,X,B,C).

animal_name(X,Y):-animal(Y,X,B,C).

animal_name(X,Y):-animal(A,X,C,Y).

animal_name(X,Y):-animal(A,X,Y,C).

/*

Queries---

1. Add a rule that writes a list of all the animal names.

Page 9: 31-40 Prolog Program

2. list all animal name which is small.

3. list all animal name which is mammal.

4. list all animal which is bird.

5. List all animal which is carnivore.

6. find the name of animal which is carnivore as well as ugly.

Solutions---

1. Add a rule that writes a list of all the animal names.

Ans.-- animal_name(X).

output-- X = tiger ? ;

X = lion ? ;

X = hyena ? ;

X = zebra ? ;

X = eagle ? ;

Page 10: 31-40 Prolog Program

X = sparrow ? ;

X = snake ? ;

X = lizard

2. list all animal name which is small.

Ans.-- animal_name(X,small).

output-- X = sparrow ? ;

X = lizard ? ;

no

3. list all animal name which is mammal.

Ans.-- animal_name(X,mammal).

output-- X = tiger ? ;

X = lion ? ;

X = hyena ? ;

X = zebra ? ;

Page 11: 31-40 Prolog Program

(16 ms) no

4. list all animal which is bird.

Ans.-- animal_name(X,bird).

output-- X = eagle ? ;

X = sparrow ? ;

no

5. List all animal which is carnivore.

Ans.-- animal_name(X,carnivore).

X = tiger ? ;

output-- X = lion ? ;

X = hyena ? ;

X = eagle ? ;

X = snake ? ;

Page 12: 31-40 Prolog Program

(16 ms) no

6. find the name of animal which is carnivore as well as ugly.

Ans.-- animal(X,Y,carnivore,ugly).

output-- X = mammal

Y = hyena ? ;

no

*/

/*Ques5:-"Write a prolog program to add two number?".*/

Page 13: 31-40 Prolog Program

/*Facts and Rules*/

start:-sum,n1.

sum:- write('Enter value of X= '),read(X),

write('Enter value of Y= '),read(Y),

S is X+Y,

write('Sum is '),write(S).

/*queries*/

/*| ?- start.

Enter value of X=50.

Enter value of Y=50.

Sum is 100

| ?- start.

Enter value of X=5.

Enter value of Y=10.

Sum is 15

| ?- start.

Enter value of X=2.5.

Enter value of Y=3.2.

Page 14: 31-40 Prolog Program

Sum is 5.7

*/

Ques6.-Program to find power of the number.

Facts and Rules :-

power(N,0,1):- !.

power(N,K,R):- K1 is K-1,power(N,K1,R1),R is R1*N.

Query :-

1.

Page 15: 31-40 Prolog Program

| ?- power(2,3,R).

R = 8

yes

2.

| ?- power(3,3,R).

R = 27

yes

3.

| ?- power(4,3,R).

R = 64

Yes

Page 16: 31-40 Prolog Program

Ques7.-Program to find factorial of a given number.

Facts and Rules :-

fact(0,1).

fact(N,R):- fact(N1,R1),N is N1+1,R is R1*N.

Query:-

1.

| ?- fact(4,R).

R = 24 ?

yes

2.

| ?- fact(5,R).

R = 120 ?

Page 17: 31-40 Prolog Program

yes

3.

| ?- fact(3,R).

R = 6 ?

yes

| ?-

Ques8.-Family problem.

Facts:-

Page 18: 31-40 Prolog Program

father_of(joe,paul).

father_of(joe,mary).

father_of(joe,hope).

mother_of(jane,paul).

mother_of(jane,mary).

mother_of(jane,hope).

male(paul).

male(joe).

male(ralph).

male(X) :- father_of(X,Y).

female(mary).

female(jane).

female(hope).

Rules:-

son_of(X,Y) :- father_of(Y,X),male(X).

son_of(X,Y) :- mother_of(Y,X),male(X).

daughter_of(X,Y) :- father_of(Y,X),female(X).

daughter_of(X,Y) :- mother_of(Y,X),female(X).

sibling_of(X,Y) :- !,father_of(Z,X),father_of(Z,Y),X\=Y.

sibling_of(X,Y) :- !,mother_of(Z,X),mother_of(Z,Y),X\=Y.

Page 19: 31-40 Prolog Program

Queries :-

1.

father_of(X,paul).

X = joe ?

yes

2.

| ?-

sibling_of(X,paul).

X = mary ?

yes

3.

| ?- daughter_of(X,Y).

X = mary

Y = joe ?

Yes

Page 20: 31-40 Prolog Program

Ques9.Program to to check planet and satellite with the following facts.

Facts :-

star(sun).

orbit(earth, sun).

orbit(moon, earth).

orbit(saturn, sun).

orbit(jupiter, sun).

orbit(ioa, jupiter).

orbit(casini, saturn).

orbit(titan, saturn).

Rules :-

planet(X) :- orbit(X, Y), star(Y).

Page 21: 31-40 Prolog Program

satellite(X) :- orbit(X, Y), planet(Y).

Queries:-

1.

| ?- orbit(X,Y).

X = earth

Y = sun ?

yes

2.

| ?- planet(earth).

yes

3.

| ?- satellite(casini).

Yes

Ques10. An example using lists to find length of a list:

Facts &Rules :-

Page 22: 31-40 Prolog Program

size([],0).

size([H|T],N) :- size(T,N1), N is N1+1.

% or size([_|T],N) :- size(T,N1), N is N1+1.

Queries :-

1.

| ?- size([1,2,3,4],N).

N = 4

yes

2.

| ?- size([bill,ted,ming,pascal,nat,ron],N).

N = 6

yes

3.

| ?- size([a, [b, c, d], e, [f | g], h], N).

N = 5

Page 23: 31-40 Prolog Program

yes