main:-
write('this is main MF').
bigger(elephant, horse).
bigger(whatle, elephant).
% this is single line comment
%2. Find hcf of two numbers
hcf(0,X,X) :- !.
hcf(X,0,X) :- !.
hcf(X, Y, Result):-
X >= Y,
X1 is X - Y,
hcf(X1, Y, Result).
hcf(X, Y, Result):-
X < Y,
Y1 is Y - X,
hcf(X, Y1, Result).
% hcf(2,8, Res)
%3. Following are the facts about size of animals
bigger(elephant, whale).
bigger(whale, horse).
bigger(horse, dog).
bigger(dog,cat).
bigger(cat, mouse).
bigger(mouse, cockroach).
bigger(cockroach, housefly).
bigger(X, Y):-
bigger(X, Z), bigger(Z, X).
/*
is elephant bigger than whale
is elephant bigger than whale
bigger(horse, elephant) % no
*/
%5
mother(sumitra, laxman).
mother(sumitra, satrughan).
husband(dasrath, sumitra).
husband(dasrath, kaikai).
husband(dasrath, kausalya).
son(X,Y).
% logic
son(A,C):-
mother(C,A).
son(A,C):-
husband(C,B).
mother(B,A).
son(A,C):-
husband(B,C).
father(B,D).
father(A,B):-
husband(A,C).
mother(C,B).
% husband(X, Y):
% who is son of kaikai?
% son(kaikai).
% who are parents of ram
% who is father of ram
% who are son of dasrath?
% main.pl
% 1. Hellow World
main:-
write('This is main MF!'), nl.
% 2. WAP in prolot to find bigger number
bigger(X,Y,Z) :-
X>Y,Z=X.
bigger(X,Y,Z) :-
X<Y,Z=Y.
% ?- bigger(2,3,Z).
% 3. WAP in prolog to find HCF of two numbers
hcf(0, X, X).
hcf(X, 0, X).
hcf(X,Y,Result) :-
X >= Y,
X1 is X-Y,
hcf(X1,Y,Result).
hcf(X,Y,Result) :-
X < Y,
Y1 is Y-X,
hcf(Y1,X,Result).
% ?- hcf(12, 18, Result). % Result = 6 .
/*
4. Following are the facts about the size of animals
*/
% Base facts
bigger(elephant, whale).
bigger(whale, horse).
bigger(horse, dog).
bigger(dog, cat).
bigger(cat, mouse).
bigger(mouse, cockroach).
bigger(cockroach, housefly).
% Recursive rule
bigger(X, Y) :-
bigger(X, Z),
bigger(Z, Y).
% Base case for termination otherwise it goes to infinite loop
bigger(X, Y) :- directly_bigger(X, Y).
% is elephant bigger than whale?
% bigger(elephant, whale). % true
% is horse bigger than elephant?
% bigger(horse, elephant). % false
% what animal is bigger than mouse?
% bigger(X, mouse).
/*
5. Following are the relation among characters of Ramayana
*/
% Base facts
mother(kaushalya, ram).
mother(kaikayi, bharat).
mother(sumitra, laxman).
husband(dashrath, kaikayi).
husband(dashrath, sumitra).
husband(dashrath, kaushalya).
% Recursive Rules
son(X, Y):-
% son using mother
mother(Y, X).
son(X, Y):-
% son using father
father(Y, X).
parents(X, Y):-
mother(Y, X).
parents(X, Y):-
mother(Z, Y),
husband(X, Z).
father(X, Y):-
husband(X, Z),
mother(Z, Y).
% i. Who is the son of kaikayi?
% son(Son, kaikayi).
% ii. Who are parents of ram?
% parents(Parent, ram). % Parent = dashrath
% iii. Who is the father of ram?
% father(Father, ram). % Father = dashrath.
% iv. who are the sons of dashrath
% son(Son, dashrath). % Son = bharat ; Son = laxman ; Son = ram.