“Constraint programming represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it.” Eugene C. Freuder, Constraints, April 1997

Introduction to PROLOG - but WHY?

When I started this blog yesterday late in the night, I just knew that I wanted to document what I know and the things that I think are cool. Today I decided to write an introduction to PROLOG. =) It seems just right to start with a programming language that is so fun!

Basic concepts

The name PROLOG comes from PROgramming in LOGic. And that is really all about it. This language is used mostly to solve problems related to objects or facts and their relationships.

In this series, I will go through some basic concepts, such as facts, questions, variables, rules and conjunctions.

Imperative programming vs. Declarative programming

There are different styles of building the structure and elements of computer programs. The imperative programming, which uses statements to change the program’s states, feels much the same way like the imperative mood in natural languages, expressing commands for the computer to perform. The focus of this paradigm is to describe how the program operates.

The declarative programming expresses the logic of the algorithm without describing its control flow. One describes what the program must accomplish, states the problem domain and sometimes adds the data structure.

Programming in PROLOG

For start programming in PROLOG, we first should to think what we want to accomplish. This will also mostly determine how we state the facts. It is very common to use a family tree as a first PROLOG problem. For it, we are going to:

  • declare facts about the subjects in question;
  • declare the known relations beyond the subjects;
  • define rules about the subjects and relationships;
  • state questions about these subjects and relationships.

Let’s do it!

We are going to build a family tree. First, we declare known facts about the subjects. In PROLOG, this would look like this:

male(abe).
male(isaac).
male(ismail).
male(esau).
male(jacob).
male(joseph).
female(sarah).

Easy, right? Now, we can declare the relations between the subjects. The relation family could look like this:

parent(abe, isaac).
parent(abe, ismail).
parent(sarah, isaac).
parent(isaac, esau).
parent(isaac, jacob).
parent(jacob, joseph).

parent takes two arguments and describe the relation between these two subjects. In this case, the fact that Abe is Isaac’s parent is written like parent(abe, isaac)..

Our program consists of 6 clauses and each of them declares a parent relation fact.

parent(abe, isaac). is also an instance of the relation family and each instance can be also called relationship. We name the set of all instances relation.

There are many different ways to declare facts and write relations in PROLOG. The arguments order is defined by you here, but it needs to be followed and therefore you should stay consistent for the whole program. Here parent(abe, isaac). means Abe is Isaac’s parent while parent(isaac, abe). means Isaac is Abe’s parent. It is on you to use meaningful names for the relations.

Next up: Queries!