PROLOG was created in 1972 and it has been used since then, mostly in relational databases, natural languages comprehension and automation.

Continuing …

Continuing from the last post, I will again start with some theory before continuing to write our family code.

Logical programming

In logical programming, we are focused on knowledge and not in the algorithms. That is also why the code sometimes looks like a database. This programming paradigm (which one could also include PROLOG) is based on formal logic, more specific based on predicate logic.

This means that PROLOG has a set of sentences in logic form, called clauses. All clauses that we wrote in the last post are facts. They are the simplest type of clauses, also known as Horn clauses.

More basic technical concepts

PROLOG doesn’t have control structures (i.e. if-else, do-while, for, switch) like most of the other programming languages. For this, we use logical methods, declaring how the program should achieve the goal.

You can run PROLOG code in interactive mode, you can state queries declaring facts and rules and you can return a solution using the unification mechanism.

In PROLOG you don’t have types. All data has the one and same type, known as term. A term can be a constant, a variable or even a concatenation of more than one term.


In PROLOG, a variable is also a term and for that, one should rather think about it as an open hypothesis than a container waiting for a concrete value. This means, we can create some undefined predicates. But this variable is just “open” or “variable” until the variable is unified (we are going to discuss it later).

There is a special variable: the anonymous variable _, that can be used even inside the same clause independently. (we will work on an example to clarify it).

Installing PROLOG

The easiest way to get started with PROLOG is installing SWI-Prolog. Now that you are all set, let’s run our PROLOG code:

  • Put the facts from the first post in a file called
  • Open SWI-Prolog from the command line. You should see something like this:
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.2)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit
For built-in help, use ?- help(Topic). or ?- apropos(Word).

  • Now we can load our facts into SWI-Prolog using consult like this:
?- consult('').

  • With our facts loaded, we can start asking questions, like “Is Isaac Jacob’s father?”. This would look like this:
?- parent(isaac, esau).

Notice that we don’t have the prompt again, we are still “inside” the query. In PROLOG, when a query returns true, you are able to check if there are more possibilities - called instances - for the same query. To check the next possible instance, you can use the “;”. It should look like this:

?- parent(isaac, esau).
true ;


false is always the end of the query for PROLOG. It means, it can not find any more instance for the query. It is also possible to make open queries like “Who are the kids of Isaac?”. This query would look like this:

?- parent(isaac,X).
X = esau ;
X = jacob.


As you see, in this case, PROLOG is not just answering true but also giving us the values that makes the question true. As we declared parent(isaac, esau). before parent(isaac, jacob). the first instance we get is also X = esau This happens because PROLOG uses a linear resolution strategy.

Go ahead and try it yourself, querying for the parents of Isaac, for example.

Next up: More queries and a bit more theory!