Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Relations



Relationship is one of the main features that we have to properly mention in Prolog. These relationships can be expressed as facts and rules. After that we will see about the family relationships, how we can express family based relationships in Prolog, and also see the recursive relationships of the family.

We will create the knowledge base by creating facts and rules, and play query on them.

Relations in Prolog

In Prolog programs, it specifies relationship between objects and properties of the objects.

Suppose, there is a statement, Amit has a bike, then we are actually declaring the ownership relationship between two objects one is Amit and the other is bike.

If we ask a question, Does Amit own a bike?, we are actually trying to find out about one relationship.

There are various kinds of relationships, of which some can be rules as well. A rule can find out about a relationship even if the relationship is not defined explicitly as a fact.

We can define a brother relationship as follows −

Two person are brothers, if,

  • They both are male.

  • They have the same parent.

Now consider we have the below phrases −

parent(sudip, piyush).
parent(sudip, raj).
male(piyush).
male(raj).
brother(X,Y) :- parent(Z,X), parent(Z,Y),male(X), male(Y)

These clauses can give us the answer that piyush and raj are brothers, but we will get three pairs of output here. They are: (piyush, piyush), (piyush, raj), (raj, raj). For these pairs, given conditions are true, but for the pairs (piyush, piyush), (raj, raj), they are not actually brothers, they are the same persons. So we have to create the clauses properly to form a relationship.

The revised relationship can be as follows −

A and B are brothers if −

  • A and B, both are male

  • They have same father

  • They have same mother

  • A and B are not same

Family Relationship in Prolog

Here we will see the family relationship. This is an example of complex relationship that can be formed using Prolog. We want to make a family tree, and that will be mapped into facts and rules, then we can run some queries on them.

Suppose the family tree is as follows −

Family Relationship

Here from this tree, we can understand that there are few relationships. Here bob is a child of pam and tom, and bob also has two children ann and pat. Bob has one brother liz, whose parent is also tom. So we want to make predicates as follows −

Predicates

parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
parent(bob, peter).
parent(peter, jim).

From our example, it has helped to illustrate some important points −

  • We have defined parent relation by stating the n-tuples of objects based on the given info in the family tree.

  • The user can easily query the Prolog system about relations defined in the program.

  • A Prolog program consists of clauses terminated by a full stop.

  • The arguments of relations can (among other things) be: concrete objects, or constants (such as pat and jim), or general objects such as X and Y. Objects of the first kind in our program are called atoms. Objects of the second kind are called variables.

  • Questions to the system consist of one or more goals.

Some facts can be written in two different ways, like sex of family members can be written in either of the forms −

female(pam).
male(tom).
male(bob).
female(liz).
female(pat).
female(ann).
male(jim).

Or in the below form −

sex( pam, feminine).
sex( tom, masculine).
sex( bob, masculine).
¦ and so on.

Now if we want to make mother and sister relationship, then we can write as given below −

Mother and Sister Relationship

In Prolog syntax, we can write −

mother(X,Y) :- parent(X,Y), female(X).
sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X), X \== Y.

Now let us see the practical demonstration −

Knowledge Base (family.pl)

female(pam).
female(liz).
female(pat).
female(ann).
male(jim).
male(bob).
male(tom).
male(peter).
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
parent(bob,peter).
parent(peter,jim).
mother(X,Y):- parent(X,Y),female(X).
father(X,Y):- parent(X,Y),male(X).
haschild(X):- parent(X,_).
sister(X,Y):- parent(Z,X),parent(Z,Y),female(X),X\==Y.
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.

Output

| ?- consult('D:/TP Prolog/Sample Codes/family.pl').
compiling D:/TP Prolog/Sample Codes/family.pl for byte code...
D:/TP Prolog/Sample Codes/family.pl compiled, 20 lines read - 3081 bytes written, 4 ms

yes
| ?- parent(X,jim).

X = pat ? ;

X = peter

yes
| ?- mother(X,Y).

X = pam
Y = bob ? ;

X = pat
Y = jim ? ;

(16 ms) no
| ?- haschild(X).

X = pam ? ;

X = tom ? ;

X = tom ? ;

X = bob ? ;

X = bob ? ;

X = pat ? ;

X = bob ? ;

X = peter

yes
| ?- sister(X,Y).

X = liz
Y = bob ? ;

X = ann
Y = pat ? ;

X = ann
Y = peter ? ;

X = pat
Y = ann ? ;

X = pat
Y = peter ? ;

(31 ms) no
| ?- 
Advertisements