an example of the logic programming paradigm.

Logic programming is a variety of what we know with declarative programming, a paradigm opposed to virtually all popular programming languages, from assembler to Python through BASIC, C or Java, which we group in the category of imperative programming.

In these languages, the programmer’s job is to establish how a problem is solved through statements. However, logic programming does not revolve around how, but what: it works in a descriptive way, establishing relationships between entities, which forces those who have language to a high level of abstraction.

an example of the logic programming paradigm

This paradigm is based on the formula “algorithms = logic + control” (the so-called Informal Kowalski Equation), which means that an algorithm is created by specifying knowledge through axioms (logic) and the problem is solved through an inference mechanism that acts on it (control).

Among logic programming languages we can highlight Prolog, Lisp or Erlang. We will use the first one as an example:

an example of the logic programming paradigm

What is Prolog?

Prolog (derived from “PROgrammation en LOGique”) was born at the beginning of the 70s, by the hand of a researcher at the University of Marseille (Alain Colmerauer), while he was leading a project on the algorithmic treatment of natural languages.

Initially it was an interpreted language until, a decade later, a compiler capable of translating Prolog into a set of instructions of an abstract machine called WAM (“Warren Abstract Machine”) was created, which made it since then in a semi-interpreted language.

an example of the logic programming paradigm

Its creator, Alain Colmerauer, explains that

“Prolog was born out of a challenge: to create a very high-level language, even if it was inefficient for computer scientists at the time.

an example of the logic programming paradigm

The efficiency then consisted in a machine executing laboriously written programs very quickly. The challenge was to be able to write the programs quickly, leaving the laborious execution to the machine “.

The areas of computing where Prolog is most used are those of artificial intelligence and aspects related to it such as machine learning, natural language processing, construction of expert systems, etc.

Prolog syntax

A Prolog program consists of a set of facts (simple affirmations) and rules (that serve to affirm the veracity of a fact based on others). The set of facts of a program is the equivalent of having a database, although in this context we speak of ‘knowledge base’.

an example of the logic programming paradigm

Facts example

es_hijo (Juanito, Juan)

sum (10,5,15)

an example of the logic programming paradigm

Once these facts are defined (the first means “Juanito is Juan’s son” and the second “15 is the sum of 10 and 5”), questions can be asked to the program, which will always end in a point, like the following:

? is_child (x, John).

This question means “Is there an X that is Juan’s son?”; the answer of the program would be positive, and it would give us all the possible answers (“Juanito” … and the rest of his brothers, if there were any and we would have defined them as facts).

Likewise, from the above, we can also establish rules, which define new relationships from relationships already given. For example:

an example of the logic programming paradigm

is_father (X, Y): – is_child (Y, X), is_man (X).

This Prolog code would correspond to the logical formula “∀x ∀y ((is_child (y, x) ∧ is_boy (x)) → is_father (x, y))”.

Prolog also has structured objects:

an example of the logic programming paradigm

en_stock (book (harry_potter_1, author (jk, rowling))).

This allows us to ask the program for various elements of the entity: “Do we have ‘harry_potter_1’ in stock?”, “Do we have any ‘jk rowling’ books in stock?”, Etc.

We do not pretend with this article more than to take a slight look at a minority approach to programming and one of its main examples. Prolog is much more complex and powerful than what we suggest here: If you’re curious, you’re in luck, because a quick visit to Google will reveal large amounts of documentation on the matter.