Javascript required
Skip to content Skip to sidebar Skip to footer

How to Draw a Family Tree in Prolog

Prolog Family Tree

Introduction to Prolog Family Tree

The prolog family tree, defined as the prolog, is a logical programming language with symbolic and non-numeric data processing. It is especially well suited for solving problems that require objects and relationships between the objects. It is a user-friendly programming language because it uses logic in its programming, prolog family trees can be constructed by using the facts, rules, and queries, collection of facts and rules is called a knowledge-base, which describe the relationship of their objects. The prolog programming is all about knowledge- base and by posing queries means by asking questions to prolog about the information stored in knowledge-base.

Syntax of Prolog Family Tree

The prolog syntax for mother-sister relationship is given as below:

Prolog Family Tree op 1

Mother Relationship

Prolog Family Tree op 2

Sister Relationship

  • mother(M,N): parent(M,N), female(M).
  • sister(M,N): parent(O,M), parent(O,N), female(M), M\= =N.

We can also define:

  • father(M,N): parent(M,N),female(M).
  • haschild(M): parent(M,_).
  • brother(M,N): parent(O,M), parent(O,N), male(M),M\==N.

Where _(underscore) indicates that it is an anonymous variable.

We can also write the syntax for some other relationships as follows:

  • grandparent(M,N): parent(M,O), parent(O,N).
  • grandmother(M,O): mother(M,N), parent(N,O).
  • grandfather(M,O): father(M,N), parent(N,O).
  • wife(M,N): parent(M,O), parent(N,O), female(M),male(N).
  • uncle(M,O): brother(M,N), parent(N,O).

How to Create a Family Tree in Prolog?

The program in prolog specifies the relationship between objects and the properties of objects; the family trees tell us how to construct a database of family. The database also contains facts and rules; let us consider the example "Sumit has a car." We can declare the original relationship between two objects where one object is Sumit. Another object is a car; if we say, "does Sumit own a car?" there are many types of relationships; some of them are ruled by using rules in the program, we can find the relationship between objects used, and it is not defined as a fact. Tree diagrams are very good in representations the information is clearly mentioned, and due to that, users can understand easily, our programs in prolog are the sets of clauses.

Brother relationship:

  • If they both are male.
  • If they have the same parent.

Suppose we have some clauses to illustrate the relationship:

  • parent(somit,komal).
  • parent(somit,manish).
  • male(komal).
  • male(manish).
  • brother(X,Y): parent(Z,X), parent(Z, Y), male(X), male(Y)

From the above clauses, we can conclude that b and c are brothers, but we can create 3 combinations that are true, combinations are (komal,komal), (komal,manish), (manish,manish) but actually (komal,komal) and (manish,manish) are not real brothers because they are same brothers.

Let us see the working of the family tree by considering an example that can be formed from the prolog family tree. We want to make a family tree and as per our definition that can be mapped into facts and rules, so that we can run some queries on them,

The sample family tree is given below:

sample

Here we are having one family tree, so from the above sample of the family tree, there are some relationships between them; from the above family tree diagram, we can say that 'C' is a child of A and B, which means that A is a parent of C and B is also a parent of C, B also has one child D, C has one brother D, whose parent is also A and B, so we can make predicates to call families as follows, C has two child E and F and F also has a child G, following above family tree we have written some clauses below, and every clause must be terminated by a full stop(.).

  • parent(A,C).
  • parent(B,C).
  • parent(B,D).
  • parent(C,E).
  • parent(C,F).
  • parent(F,G).

From the above examples, we can illustrate some important points that we can define parent relation by taking n-number of objects which is based on the given information of family tree so that user can easily pose the query to prolog system about relations which are defined in programs, the prolog programs terminated by clauses and that consists of clauses, the arguments of relations are of a constant type or related to any general object as we given above, objects are of atom or variables types.

Examples of Prolog Family Tree

Different examples are mentioned below:

Example #1

Knowledge base

Code:

female(pammi).
female(lizza).
female(patty).
female(anny).
male(jimmy).
male(bobby).
male(tomy).
male(pitter).
parent(pammi,bobby).
parent(tomy,bobby).
parent(tomy,lizza).
parent(bobby,anny).
parent(bobby,patty).
parent(patty,jimmy).
parent(bobby,pitter).
parent(pitter,jimmy).
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.
Input:
parent(X,jimmy).
mother(X,Y).
haschild(X).
sister(X,Y).

Output:

Knowledge base

Above is the sample prolog program; the prolog program has three sections, mainly domains, the second one is the predicate, and the third is clauses, under domain declaration can be done, so under the predicate section we can declare all the predicates in which the clauses will be defined, so we have given names in the above program, we have also declared some knowledge base, the clauses are the facts in prolog program, we have taken some facts, and some rules will be the members of the respective clauses to show the relationship between parent, mother, and about has child, it is a knowledge base family tree, the outputs are as shown in the screenshot.

Example #2

Code:

female(trisha).
female(joseph).
male(rahil).
male(karim).
male(kabil).
male(riya).
parent(rahil,karim).
parent(rahil,kabil).
parent(karim,trisha).
parent(karim,joseph).
son(X,Y):-male(X) ,parent(Y,X).
daughter(X, Y):- female(X) ,parent(Y ,X).
sibling(X, Y):-
parent(Z, X) ,parent(Z,Y), X \= Y.
Input:
son(karim,rahil).
son(joseph,karim).
daughter(trisha,kabil).
daughter(riya,kabir).
sibling(karim,kabil).

Output:

Prolog Family Tree 5

In the above program, we have illustrated the family tree in the form of facts and rules; we have written rules to get son, daughter, and sibling so that to describe the relationship between objects.

Conclusion

In the above article, we conclude that in prolog relation, the specification is between the objects and the properties of the object, as we have seen in this article we can make relationship easily by making family trees and illustrating them, in prolog program the family trees can be illustrated by taking facts, rules and by posing queries.

Recommended Articles

This is a guide to Prolog Family Tree. Here we discuss the introduction, how to create a family tree in Prolog? and examples, respectively. You may also have a look at the following articles to learn more –

  1. How Artificial Intelligence Works?
  2. Web Programming Languages
  3. sprintf Python
  4. Prolog Programming

hookebeeked.blogspot.com

Source: https://www.educba.com/prolog-family-tree/