Prolog Tutorial

By Sadi Evren SEKER

Aim of this handout is a quick startup to prolog, for the programmers, starting Prolog. All the examples in this tutorial are from standard prolog and they have been tested in swi-prolog[1] under both Linux and MS-Windows environments. For any comments, objections or suggestions please contact me by Also a soft copy of this tutorial is in the following link:

1. Startup and shutdown of prolog:

First of all prolog is an interpreter and like most of the interpreters it has a command line for inputs and outputs. You can start this command line by simply executing the interpreter.


bash# pl


C:\prolog> pl

This executable pl starts the prolog interpreter. Now you can enter any command by using the command line of prolog. In Linux or Windows, rest is exactly same. Our first command is “halt” , the exit command. But just before it, keep it in your mind that every command in Prolog should end with a “.” –dot-. So our close statement is as following:

?- halt.

2. Predicates in Prolog:

Every input which ends with a “.” is either a predicate or a command in Prolog. Also commands are predicates too but in this level let’s make an innocent distinction. For example in the previous example “halt.” was a command and it has been executed by Prolog. But what would happen if we enter a predicate, which has not been pre-coded? Answer is simple it would be assumed as a new predicate.

But there is a very important issue we should cover before starting predicate examples. There are two different modes in Prolog. Since it is an interpreter, you can not input all the information from command line. Some of operations should be done on a file. For example inserting a new predicate should be done by using a file. So we should see a file load operation and predicates at the same time. Lets look at the following example.

First we have prepared the following file:



















All the prolog files have the extension, “.pl”. Now let’s name this file as “”. I assume that you have set the environment variable path correct so keep this file in your working directory. Now let’s start and load the file to prolog.

?- [ilk].

% ilk compiled 0.00 sec, 1,920 bytes



Okay, “[].” Is a command in prolog to load a file inside the brackets. Prolog seeks for the file with “.pl” extension with the name inserted inside the brackets (notice the “.” at the end of the line).

Now to be sure that it is loaded let’s try a simple test.

?- yuklu.



“yuklu.”, is not a pre-coded prolog command. It is inserted by our file and after this insertion we can easily test in the command line and see the “Yes” answer. If you enter the same command before loading the file you would end up with a “No” answer.

Let’s continue with the rest of the file. We have a small database implementation in our file. Prolog is a relational database by the nature. We have students, instructors, lectures and two relations, the takes relation and lecturer relation. Now lets test the first predicate.

?- student(ali).



In fact this line has no difference from the previous line “yuklu.”. But as you can easily notice there are brackets around ali. In this example student is the name of predicate and ali is the case that this predicate applied. There are other cases that this predicate is applied. Please look at the following examples.

?- student(veli).


?- student(ender).


?- instructor(ender).


?- student(X).

X = ali ;

X = veli ;

X = sadi ;



In the above example you can see some test. First we check whether veli is a student or not, then apply same check on ender. In the third example we check ender as an instructor. The fourth example is somehow different than the rest of the examples. First of all any name starting with a capital letter in Prolog is assumed as a variable. For example, X, Y, Z, XXX, Xxx, XxX are all different variables in Prolog. So we can say prolog is a case sensitive language indeed. What happened in fourth example is very interesting. We have the student predicate with a variable, this forces Prolog to fulfill the variable. What can fulfill the predicate student? Answer is ali, veli, sadi. You need to press “;” in keyboard to continue to see the results. After finishing all possibilities prolog says “No” in the meaning of there is no more fulfillment for the predicate student.

Now let’s go further and see what are the lectures that ender instructs.

?- lecturer(X,ender).

X = ai ;

X = ics211 ;



Output is obvious. We could make a query like who instructs the ics211 as following.

?- lecturer(ics211,X).

X = ender ;

X = ali ;



Now let’s go one step further and try to find a solution for the following query. Find the instructor of ali. What should be our first step, since there is no direct relation between student and instructor. We have a indirect relation between student and instructor by using lecture. Ok let’s see the solution.

?- takes(ali,X),lecturer(X,Y).

X = ai

Y = ender ;



Since we know that ali takes a lecture (we do not know the name of lecture infact). In addition to this we know there is an instructor on this lecture. Now lets make a connection between lecturer and takes. Only way to connect this predicates is the lecture. So our solution is this : “ali takes a lecture that the lecturer of this lecture is who?” In this English statement we mean the same lecture in both cases. So if we translate this to Prolog, we end up with the above example. In this example we have used X twice, first for the lecture of ali, second for the lecturer of this lecture, and both should be same. And we have used another variable Y for the instructor of the lecture to get the answer.

Now let’s look at the predicates named grade. Grade is something shown by using capital letters. What is the solution to use capital letters as a data instead of a variable. In addition to this there are two syntaxes for grade. Please study the following examples.

?- grade(X).

X = 'A' ;

X = 'B' ;


?- grade(X,Y).

X = 'A'

Y = 4 ;

X = 'B'

Y = 3 ;


?- grade('A',X).

X = 4 ;


?- grade(X,4).

X = 'A' ;



In the above examples you can see that grade(_) and grade(_,_) are two different predicates in fact. You can think the difference as overloading in object oriented programming. You overload the same function by using different variable sets and at the end the function executed is selected by using this variable sets. In this example if you give a single variable, prolog goes to grade(_) and if you enter two variable prolog switches to grade(_,_).

3. Variables in Prolog

We should start to understand variables in Prolog by understanding atoms. Atoms can be in 3 forms:

  1. letters

  2. special characters

  3. strings in quotes

In the letter form, we can see the following examples:












In the special character form, we can see the following examples.





In the strings form we can see the following examples.







Please notice the last two examples in this case. They have double quotes at the beginning and at the end. This means that they are in a strings form but they will be considered as a list of numbers. (numbers here are the ascii code of each character). We will cover them in the lists.

We have already covered some examples which include some of the above atoms. Now let’s look at the variables in Prolog.

As I have mentioned before, every atom, starting with a capital letter is considered as a variable in Prolog. But there is an exception for this rule. I it called unbounded variable and used as “_” the underscore sign. First lets look some variable examples than I will try to give some examples from their usages.










Now please study the following example:

?- takes(X,_).

X = ali ;

X = veli ;



We have 3 students, ali, veli, sadi, but some has classes and some does not. How can we see the students attending classes this semester? Above line is the answer of this question. We say that give me the names of students, taking a course, and don’t care the name of classes they are attending. So “_” means do not care anything about the value of this variable. Any thing starting with “_” means the same thing indeed. More sophisticated usage of “_” will be covered in the following chapters.

4. Day of Week Example in Prolog

Let’s start with the following code piece








%day of week, verilen ay gun ve yýldaki gunun karsiligini donduruyor


A < 3,

I is A+12,

M is Y-1,!,



A is U+1,

I is K//400,

J is K//100,

T is K//4,

L is I+T+6+K-J,

P is A*26,

O is P//10,

R is L+O+G,H is R mod 7,



%Isim is (G + 26 * A / 10 + Y + Y / 4 - Y / 100 + Y / 400 + 6) mod 7,!.

Let’s start with the simplest. In Prolog file you can add your comments by using “/* … */” for blocks or “%” for single lines.

Next thing we are not familiar with is the definition of a function. In prolog, functions are declared by “:-“. For example in previous examples we have written many predicates. Each one of this predicates was a function in fact. But they do not have any function declaration, or function body. Okay, we have declared a function called “dow” with 3 parameters. Notice that we have 2 functions with the same name and in Prolog they are executed by the order in the file. (this is useful for the priorities of predicates or functions).

Okay, let’s try to understand these functions. First of all we are trying to find the day of week by using these functions. Our in put is the day, month and year in number format and our output is the name of day in string format. So you can see that G,A,Y are the three inputs of our program and Isim is the output of our program. So we give the output as a input to the function and Prolog finds the answer that will be placed in this empty variable, else it will do a Boolean operation, like Yes or No to check it is validity.

Our first line in the first function is “A < 3, “ this is a comperrision line in prolog, so we can understand that if A is smaller than 3 prolog will continue to use this function else prolog will not continue to this function and jump to the next function available. Let’s assume we are looking for the day 30/01/2002, than Prolog will continue to the first function.

In prolog we do not have a assign and store operator. For example in C, you can use the following line


To increment the value of X. But in Prolog we use “is” as an assignment operator. In fact the equal symbol “=” is used for compressions between two variables, to check whether they are equal or not. So the line

I is A + 12

Means that take the value of A and 12 to this value and store the result in variable I. By the same way the line

M is Y-1

Means that get the value of Y decrease it by one and store the result in the variable M.

“!” is a very important operator in Prolog. As we have mentioned before in the example of student(X). Prolog had returned more than one solution by nature. What will happen if we have more than one solution for such an expression in our function. Prolog will execute on a set of results instead of a single solution. That’s what we do not want. Solution is simple we use the “!” (it is named as cut operator in many of the references).

And the last line in the first function is the


This line is obvious, it calls the function dow again. Please notice that after execution of the first dow function it is impossible to end up with an I which has a value smaller than 3. So this line forces our program to go to the second dow function.

In the second dow function there is nothing interesting except the operator “//”. This operator find the reminder of division operation. And in the line


we use something like a database again to find the name of the related number. Return operation in a function is very simple again. There is no word like return in Prolog. It returns the value when you assign something in it. In this case Isim is our return parameter and when we assign something in it this function returns this name. Again we have used the cun operator (“!”), to hinder the return of more than one results.

Finally, I want to care you about the following line:


the above line says that, p is true only if p is true. And if you type the following line:


Prolog will go into an infinite loop.

We will continue with the Lists in Prolog, Input Output in Prolog and the Debugging in Prolog next week.


[1] Swi-Prolog :

[2] MS-Windows is a trademark of Microsoft Co.

[3] I have learned Prolog from the book, PROLOG Programming for Artificial Intelligence by Ivan Bratko, there is no direct similarity between this handout and the book but there may be some indirect similarities.