Skip to content

an example of the logic programming paradigm

23 mayo, 2021

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 languages a high level of abstraction.

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:

The programmer career in 2017 and in the future (with Javier Santana)

What is Prolog?

Prolog (derived from “PROgrammation in 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 directing a project on the algorithmic treatment of natural languages.

20 years later I want to re-program: what big changes have there been and where to start

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.

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.

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.

Visual Prolog

Screenshot of the Visual Prolog IDE. Image: Prolog Development Center A / S, Denmark (via Wikipedia).

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’.

Facts example

es_hijo (Juanito, Juan)

sum (10,5,15)

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).

So you can learn to use PyTorch, the most accessible tool for creating neural networks

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:

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:

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

The above 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 hereIf you are curious, you are in luck, because a quick visit to Google will reveal large amounts of documentation on the matter.