We can easily express relationships in Prolog. These relationships exist between objects and their properties. For example, if we say, Tina has a car, then, it means Tina is the owner of a car. Here, owner is the relationship that exist between two objects- Tina and car.
When we pose a query, “Does Tina own a car?”, we are actually trying to find out whether this relationship exists or not. Some relationships are expressed using rules. A rule can find out about a relationship even if it is not defined explicitly as a fact.
Create a new file kb3.pl and write the following facts and rules in it. These are used to find whether two persons are sisters to each other or not.

Now, open GNU-Prolog and find out between which two individuals this relationship exists.

Credit: Copyright (C) 1999-2021 Daniel Diaz
You can add information about more individuals int it. You can even define more relationships. For example,
- mother(X,Y):- parent(X,Y), female(X).
- brother(X,Y):- parent(Z,X), parent(Z,Y), male(X), X \== Y.
- haschild(X):- parent(X,_).
- grandparent(X,Y):- parent(X,Z), parent(Z,Y).
- grandmother(X,Z):- mother(X,Y), parent(Y,Z).
- grandfather(X,Z):- father(X,Y), parent(Y,Z).
- wife(X,Y):- parent(X,Z),parent(Y,Z), female(X),male(Y).
- uncle(X,Z):- brother(X,Y), parent(Y,Z).
In Prolog we can trace the execution to see how output was obtained. To enter into the trace mode, type “trace” in the Prolog console.
Data Objects
In Prolog, we can divide data objects into the following categories.

Below are some examples of different kinds of data objects:
- Atoms −
- Numbers − 100, 1235, 2000.45
- Variables − X, Y, Xval, _X
- Structures − day(9, jun, 2017), point(10, 25)
Atoms: Atoms are like constants. They can be any names or objects. For example, amyra, num_of_digits, c3, etc. While using atoms, we must consider the following points:
- An atom can be a string of alphabets, digits and the underscore character, ‘_’.
- It should start with a lower-case letter.
- An atom can be a string of special characters that already have a predefined meaning. For example, :-’ <—>, =======>, …, .:., ::=
- An atom can be a string of characters enclosed in single quotes. This is important when we have an atom that starts with a capital letter. By enclosing it in quotes, the string becomes easily distinguishable from variables. For example, ‘Delhi’, ‘Narendra_Modi.
Numbers: They represent values that can be integers like 100, 4, -81, 2022. In Prolog, range of integers is from -16383 to 16383. Numbers can also be real numbers, but floating-point numbers are rarely used in Prolog programs as Prolog is usually used for symbolic, non-numeric computation. For example, 3.14159, -0.00029, 123.45, etc., are real numbers.
Defining Variables in Prolog
Like every programming language, Prolog also lays some rules to define new variables. These rules states,
- Variables are strings of letters, digits and underscore characters.
- They start with an upper-case letter or an underscore character.
Examples of valid names of variables are X, Val, Student_name, Shoppinglist, _a50, _39.
Anonymous Variables in Prolog
Anonymous variables have no names. They are written using a single underscore character ‘_’. And each anonymous variable is treated as different. They are not same. These variables are very useful in certain situations. For example, if we have facts:
“John plays cricket” and “Shivansh plays tennis”, then we can easily find out who plays cricket using an ordinary variable. But, if the task is to find whether anyone plays cricket. Here, the name of the person who plays cricket is not required, so in such a case we must use anonymous variables.
In general, anonymous variables are used when there is a need to use a variable without knowing its value. Look at the code given below that demonstrates the use of anonymous variables. Here, we have used knowledge base 1 to find whether we have any student in the KB. Note that the console replies only ‘yes’ or ‘no’, I does not show names (or values) of the students.

Credit: Copyright (C) 1999-2021 Daniel Diaz
Comparison Operators
Comparison operators are used to compare two equations or states. Some comparison operators are listed in Table.

Let us try these operators in GNU-Prolog now.


Leave a Reply