The Logical Programming Paradigm

Schrodingdong
ENSIAS IT
Published in
4 min readDec 6, 2021

For the majority of us programmers, we have been introduced to programming as a way to do things by doing other small things in an ordered and sequential manner. In fact, many tasks in our daily lives are just that ! Let’s say that both of us are hungry and we wanted to bake a cake, we will follow the steps to make it :

1- Check that we have all the necessary ingredient.

2- Preheat the oven

3- Mix in a bowl sugar and butter, eggs …

The strategy and the methodology to solve a problem is what is known in programming as programming paradigms, and there is a bunch of them that can be separated into 2 major categories : Imperative programming & Declarative programming, and each of which has sub-categories as shown in this diagram :

Source : https://www.geeksforgeeks.org/introduction-of-programming-paradigms/

Imperative programming paradigm involves changing the state of the machine and the program by executing statements and tasks step by step, letting us manipulate the control flow of the program. It puts emphasis on “How does the code work”.

Declarative programming paradigm on the other hand is a way to make programs by only stating the logic of computation. It puts emphasis on “What does the code do”.

In this article, we will focus on the Logical Programming Paradigm, so what is it then ?

Logical Programming Paradigm uses predicates and logical statement like facts and rules, as a base data to answer a given query. It uses an abstract model similar to mathematics’ logic statements to deal with objects and their relationships. So, the first thing that we should do then is to establish a logic that bounds the stated facts and rules, writing the code is just a matter of explicitly expressing these statements in the chosen language syntax.

Some of the popular logical programming languages are : Prolog, Datalog, ASP …

But why should you get interested by learning about logical programming ?

One of the major pros of this paradigm is that it is generally easier to write, read and understand, making it easier to maintain and reduces the cost to do so. In order to create a procedure in C, we should explicitly declare what we should do in each step, and the compiler will execute them in their given order. But in a logic programming language, all we need to write is the necessary conditions to get a true or false predicate, and with some techniques like recursion, we can create loops and more complex statements … It can be used for Predictive analysis : logic languages can search for inconsistencies in order to make predictions using large data sets, like predicting failures of industrial machines; or Natural language processing : that allows for better interactions between humans and computers.

Another speculative point is that Logic programming could be the future of programming in general. We can all try to imagine a world in which the only things needed to input in a machine to compute something are the starting assumptions, a goal not yet reached but hopefully not so far away !

Okay that sounds exciting, but how does a logical code look like ?

The most popular declarative logical programming language, and the one we will discover is Prolog : its name is a fusion between PROgramming and LOGic. This language is based around 3 elements :

Facts − The fact is a predicate that is true. (Example : food(Burger). )

Rules − Rules are extinctions of facts that contain conditional clauses. To satisfy a rule these conditions should be met. (Example : Predicate(args) :- Expr1 , … , ExprN. : can be interpreted as : if Expr1 … ExprN are true, then the predicate is true)

Questions − And to run a prolog program, we need some questions, and those questions can be answered by the given facts and rules.

Example program and execution :

food(Burger).

food(Pizza).

Simple Prolog example program

- , : the logical “and” (Example : in “predicate1(args) , predicate2(args)” the ‘,’ is the logical “and”.)

- constants begin with lowercase letters.

Here we have 6 predicates that can be summed in this graph :

For example, father(amine, ahmed) can be interpreted as : amine is the father of ahmed.

For the last Rule siblings(A,B) : A and B are siblings if they have the same parent or the same mother. As the output suggests, khawla and ahmed are indeed siblings !

Conclusion

As a paradigm, logical programming has both pros and cons. Its related languages may not be as fast as others, but it can be more practical to formulate a solution to a problem using them. Generally speaking, understanding paradigms is a key component to make a programmer a polyglot : Once you understand a paradigm associated to a language, the only thing left to do is pick up its syntax !

Sources and further reading:

https://www.virtusa.com/digital-themes/logic-programming

https://www.geeksforgeeks.org/introduction-of-programming-paradigms/

https://www.youtube.com/watch?v=cgVVZMfLjEI&ab_channel=CodingTech

https://www.tutorialspoint.com/prolog/prolog_introduction.htm

http://athena.ecs.csus.edu/~mei/logicp/prolog/programming-examples.html

https://www.computerhope.com/jargon/l/logic-programming.htm

--

--

Schrodingdong
ENSIAS IT

596F75206861766520746F6F206D75636820667265652074696D65