You are currently viewing Program to Define Relations in Prolog

In previous article we have seen how to find the reverse of a list. The following program shows the implementation of rules. It describes the relationships of brother, sister, grandparents, son, daughter and uncle in a family. The facts used here include male, female, parents, and father.

Relations to be define:-

1) Brother

2) Grandparents

3) Son

4) Sister

5) Daughter

6) Uncle.

PROGRAM:

male(a).

female(b).

male(c).

female(d).

parents(a,m1,f1).

parents(b,m1,f1).

parents(c,m1,f1).

parents(d,m2,f2).

parents(f1,f5,f6).

parents(f2,f5,f6).

parents(f3,f7,f8).

parents(f4,f7,f8).

father(f1,a).

father(f2,b).

father(f3,c).

father(f4,d).

brother(X,Y):-male(X),parents(X,M,F),parents(Y,M,F).

sister(X,Y):- female(X),parents(X,M,F),parents(Y,M,F).

son(X,F):-male(X),father(F,X).

daughter(X,M,F):-female(X),parents(X,M,F).

uncle(Y,X):- male(X),father(M,X),parents(M,M1,F1),parents(Y,M1,F1).

grandparents(M,F1,X):-father(F,X),parents(F,M,F1).

 

OUTPUT:

 

Dinesh Kumar Bansal

Dinesh Kumar Bansal is an Indian Software Developer, who is working on ASP.Net, MS-SQL, SAP-ABAP technologies from last one year. His Basic Principle of developing software is, “You have to clear about, what do you want to do, how can it be done”. He always says that development can be done with a cool & fresh mind.

Leave a Reply