Extra Example For Prolog

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Extra example for prolog

Q1: How many facts, rules, clauses are there in the following
knowledge base?

parent(john, carol).
male(john).
parent(alice, jerry).
male(jim).

male(jerry). parent(alice, carol).

female(sue). parent(sue, ted).

female(carol). parent(jerry, ted).

female(alice). parent(carol, bob).

parent(john, jerry). parent(jim, bob).

sib(X, Y) :- parent(Z, X), parent(Z, Y), X \== Y.

brother(X, Y) :- sib(X, Y), male(X).

grandparent(X,Y):-parent(X,P) , parent(P,Y).

Number of facts are : 14


Number of rules are : 3
Number of clauses are : 17

Q2: Define Rule to :


a) Father relationship.
father(X,Y):- parent(X,Y),male(X).
b) Sister relationship.
sister(X,Y):- sib(X,Y), female(X).
c) Cousin relationship.
cousin(X,Y):- grandparent(Z, X), grandparent(Z, Y) , X\==Y.
OR
cousin(X,Y):- parent(P, X) , parent(S, Y) , sib(P, S).
d) Uncle relationship.
uncle(X, Y):- parent(Z, Y), sib(X , Z), male(X).
e) niece relationship.
niece(X,Y):- parent(Z, X), sib(Y , Z).
f) Mother relationship.
mother(X,Y):- parent(X,Y), female(X).
f) Daughter relationship.
daughter(X,Y):- parent(Y,X), female(X).

Q3: How does Prolog respond to the following queries ?(Family


relationship file contains all rules and fact in Q1 & Q2)
1- father(z , john).
False
2- sister( jerry , carol).
False
3- sister(carol , jerry).
True
4- mather(M , ted).
M=sue
5- cousin(Y , bob).
Y=ted
6- parent(alice , jerry) , father(alice, jerry).
False
7- parent(alice , jerry) ; father(alice, jerry).
True

Q4: Write prolog rule to :


a) Test a number, if a number is even print number plus 1 else print number.
even(X):- 0 is X mod 2 , write ( X+1); write(X).
b) Sum of two number .
sum(X,Y,R):- R is X+Y.
c) Test a number, if a number is negative print number multiply with 2 else
print number minus 1.
negative(X):- X < 0 , write ( X * 2) ; write(X - 1).

You might also like