DEV Community is a community of 699,510 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

PRObably the Language Of God

Android Developer @FlixBus 🚍 Author @Pluralsight ✎

During my university days, when I was studying Mathematics and CS, software development was a refreshment of applied science in a sea of theoretical math. But then in my 4th semester came Prolog. For those who hadn’t heard of Prolog, it’s the most inapplicable programming language in the universe (at least that’s what I thought). Created in the early 1970s, it was one of the first logic programming languages. It’s the most mathematical thing in the world of computer science.

Seven years after my first encounter with Prolog, I found some old .pl files, I opened them and was amazed that at one point in my life I had understood and more surprisingly had written those lines. So I decided to take one week of my life, and check again if Prolog is worth learning in the 21st century. At that point, I created GitHub repository, downloaded Prolog compiler, and started.

The main building blocks of Prolog code are facts and rules. A fact is something like an axiom, a statement that is taken to be true. Meanwhile, a rule is analogous to a theorem, and its truthiness can be derived from facts.

These are the examples of a fact and a rule. The next step would be to load a file containing facts and rules into Prolog and make queries on it. For this case, the examples of queries would be:

?- guitarist("Saul Hudson").
true.
?- musician("Saul Hudson").
true.
?- guitarist(X).
X = "Saul Hudson".

As you can see, guitarist does not behave like a function. That’s because it’s not a function, it’s a relation. Prolog is a relational programming language. To better explain this, let’s take a look at the following example:

factorial defines a relation between some natural numbers. What does that mean? The fact factorial(1,1). means that 1 is in relation factorial with 1. And what about the second line?! Well, it can be read like this: “Numbers N and R are in relation factorial if N>1 and N1 is N-1 and N1 and R1 are in relation factorial and R is N*R1” 😧. Could that be expressed in an understandable way?! In practice, it means that if you query this set of rules, you can calculate factorial.

?- factorial(5,R).
R = 120 .
?- factorial(1,R).
R = 1 .
?- factorial(20,R).
R = 2432902008176640000 .

These are a couple of beginners examples of Prolog code. I hope this is sufficient to show you how Prolog works, and what it has to offer. If you find this interesting, and if you are curious enough to do something on your own, feel free to add it to the repository.

So, should you learn Prolog? Well, it’s the 21st century, and you should be learning whatever you can find on the internet. But seriously, is it worth learning? I think it is. It’s a niche concept that never really took off, but it somehow feels fresh and modern. For you, it will probably be a totally new approach to software development, that will get you out of your comfort zone, and force you to think in a different way while giving you another perspective on how software can be written.

Thanks for reading! If you enjoyed this story, please click the ❤️ button and share to help others find it! Feel free to leave a comment 💬 below.
Have feedback? Let’s be friends on Twitter.