Prolog - Basics


In this chapter, we will gain some basic knowledge about Prolog. So we will move on to the first step of our Prolog Programming.

The different topics that will be covered in this chapter are −

Knowledge Base − This is one of the fundamental parts of Logic Programming. We will see in detail about the Knowledge Base, and how it helps in logic programming.

Facts, Rules and Queries − These are the building blocks of logic programming. We will get some detailed knowledge about facts and rules, and also see some kind of queries that will be used in logic programming.

Here, we will discuss about the essential building blocks of logic programming. These building blocks are Facts, Rules and the Queries.


We can define fact as an explicit relationship between objects, and properties these objects might have. So facts are unconditionally true in nature. Suppose we have some facts as given below −

  • Tom is a cat

  • Kunal loves to eat Pasta

  • Hair is black

  • Nawaz loves to play games

  • Pratyusha is lazy.

So these are some facts, that are unconditionally true. These are actually statements, that we have to consider as true.

Following are some guidelines to write facts −

  • Names of properties/relationships begin with lower case letters.

  • The relationship name appears as the first term.

  • Objects appear as comma-separated arguments within parentheses.

  • A period "." must end a fact.

  • Objects also begin with lower case letters. They also can begin with digits (like 1234), and can be strings of characters enclosed in quotes e.g. color(penink, ‘red’).

  • phoneno(agnibha, 1122334455). is also called a predicate or clause.


The syntax for facts is as follows −



Following is an example of the above concept −



We can define rule as an implicit relationship between objects. So facts are conditionally true. So when one associated condition is true, then the predicate is also true. Suppose we have some rules as given below −

  • Lili is happy if she dances.

  • Tom is hungry if he is searching for food.

  • Jack and Bili are friends if both of them love to play cricket.

  • will go to play if school is closed, and he is free.

So these are some rules that are conditionally true, so when the right hand side is true, then the left hand side is also true.

Here the symbol ( :- ) will be pronounced as “If”, or “is implied by”. This is also known as neck symbol, the LHS of this symbol is called the Head, and right hand side is called Body. Here we can use comma (,) which is known as conjunction, and we can also use semicolon, that is known as disjunction.


rule_name(object1, object2, ...) :- fact/rule(object1,
 object2, ...)
Suppose a clause is like :
P :- Q;R.
This can also be written as
P :- Q.
P :- R.

If one clause is like :
P :- Q,R;S,T,U.

Is understood as
P :- (Q,R);(S,T,U).
Or can also be written as:
P :- Q,R.
P :- S,T,U.


happy(lili) :- dances(lili).
hungry(tom) :- search_for_food(tom).
friends(jack, bili) :- lovesCricket(jack), lovesCricket(bili).
goToPlay(ryan) :- isClosed(school), free(ryan).


Queries are some questions on the relationships between objects and object properties. So question can be anything, as given below −

  • Is tom a cat?

  • Does Kunal love to eat pasta?

  • Is Lili happy?

  • Will Ryan go to play?

So according to these queries, Logic programming language can find the answer and return them.

Knowledge Base in Logic Programming

In this section, we will see what knowledge base in logic programming is.

Well, as we know there are three main components in logic programming − Facts, Rules and Queries. Among these three if we collect the facts and rules as a whole then that forms a Knowledge Base. So we can say that the knowledge base is a collection of facts and rules.

Now, we will see how to write some knowledge bases. Suppose we have our very first knowledge base called KB1. Here in the KB1, we have some facts. The facts are used to state things, that are unconditionally true of the domain of interest.

Knowledge Base 1

Suppose we have some knowledge, that Priya, Tiyasha, and Jaya are three girls, among them, Priya can cook. Let’s try to write these facts in a more generic way as shown below −


Note − Here we have written the name in lowercase letters, because in Prolog, a string starting with uppercase letter indicates a variable.

Now we can use this knowledge base by posing some queries. “Is priya a girl?”, it will reply “yes”, “is jamini a girl?” then it will answer “No”, because it does not know who jamini is. Our next question is “Can Priya cook?”, it will say “yes”, but if we ask the same question for Jaya, it will say “No”.


GNU Prolog 1.4.5 (64 bits)
Compiled Jul 14 2018, 13:19:42 with x86_64-w64-mingw32-gcc
By Daniel Diaz
Copyright (C) 1999-2018 Daniel Diaz
| ?- change_directory('D:/TP Prolog/Sample_Codes').

| ?- [kb1]
compiling D:/TP Prolog/Sample_Codes/ for byte code...
D:/TP Prolog/Sample_Codes/ compiled, 3 lines read - 489 bytes written, 10 ms

| ?- girl(priya)

| ?- girl(jamini).

| ?- can_cook(priya).

| ?- can_cook(jaya).

| ?-

Let us see another knowledge base, where we have some rules. Rules contain some information that are conditionally true about the domain of interest. Suppose our knowledge base is as follows −


listens_to_music(ananya) :- sing_a_song(ananya).
happy(ananya) :- sing_a_song(ananya).
happy(rohit) :- listens_to_music(rohit).
playes_guitar(rohit) :- listens_to_music(rohit).

So there are some facts and rules given above. The first two are facts, but the rest are rules. As we know that Ananya sings a song, this implies she also listens to music. So if we ask “Does Ananya listen to music?”, the answer will be true. Similarly, “is Rohit happy?”, this will also be true because he listens to music. But if our question is “does Ananya play guitar?”, then according to the knowledge base, it will say “No”. So these are some examples of queries based on this Knowledge base.


| ?- [kb2].
compiling D:/TP Prolog/Sample_Codes/ for byte code...
D:/TP Prolog/Sample_Codes/ compiled, 6 lines read - 1066 bytes written, 15 ms

| ?- happy(rohit).

| ?- sing_a_song(rohit).

| ?- sing_a_song(ananya).

| ?- playes_guitar(rohit).

| ?- playes_guitar(ananya).

| ?- listens_to_music(ananya).

| ?-

Knowledge Base 3

The facts and rules of Knowledge Base 3 are as follows −


likes(priya,jaya) :- can_cook(jaya).
likes(priya,tiyasha) :- can_cook(tiyasha).

Suppose we want to see the members who can cook, we can use one variable in our query. The variables should start with uppercase letters. In the result, it will show one by one. If we press enter, then it will come out, otherwise if we press semicolon (;), then it will show the next result.

Let us see one practical demonstration output to understand how it works.


| ?- [kb3].
compiling D:/TP Prolog/Sample_Codes/ for byte code...
D:/TP Prolog/Sample_Codes/ compiled, 5 lines read - 737 bytes written, 22 ms
warning: D:/TP Prolog/Sample_Codes/ redefining procedure can_cook/1
         D:/TP Prolog/Sample_Codes/ previous definition
| ?- can_cook(X).

X = priya ? ;

X = jaya ? ;

X = tiyasha

| ?- likes(priya,X).

X = jaya ? ;

X = tiyasha

| ?-