Have You Heard of Prolog?
By: Fernando Doglio
Tags: AI, Software Development, Programming, Prolog, Software Engineering

Who’s Actually Using Prolog?

Trust me, Prolog is still being used — just not as extensively as some of the most commonly used languages in our industry, and there is a very good reason for that. But before getting into that, let me quickly explain what Prolog is in case you haven’t experienced it in the past.

What is Prolog?

Prolog is a logic programming language, which essentially means it is written in a declarative way (as opposed to using an imperative approach available in other common languages). You use relations, facts, and rules to specify your business logic and then you just query your data over those facts.

It is definitely a different way of working, but you might learn (if you delve deep enough into Prolog) that it makes solving some problems really easy.

With Prolog, you start by defining the desired state of your world, also known as a Predicate (a fact), and then ask Prolog to try to achieve that state.

For example, let’s play a game. Pretend you have these magic numbers: 1 , 2 , 4 , and 5 . You want to be able to find all solutions to a simple “add” operation using two of those numbers. Think about it like this:

Given 1 is a magic number

Given 2 is a magic number

Given 4 is a magic number

Given 5 is a magic number Which numbers can I add together that will result in 6?

Of course, we read those statements and can solve it mentally, but if you wanted to implement it using one of the most “common” languages, you’d have to implement some kind of algorithm that would test all possible couples, evaluate their results, and keep only the ones that match.

With Prolog, however, the solution is that pseudo-code I just wrote, simply written with other words:

magicNumber(1)

magicNumber(2)

magicNumber(4)

magicNumber(5)

?- magicNumber(X), magicNumber(Y), plus(X, Y, 6)

Those first few lines are identical to me saying which ones are “magic numbers,” and then you simply state, “Given my magic numbers, on variables X , and Y , which ones can I use considering that together they’ll add up to 6 ?”

The result:

1, 5

2, 4

5, 1

4, 2

We can keep making things more complicated to avoid repeated solutions, but this should be complex enough to give you an idea of the power of logic programming. It takes a while to wrap your head around it since you’re not solving the problem. You’re only stating the rules of your world (i.e. which numbers are magic) and then stating the desired solution. The middle step, the one you keep thinking about implementing, is done by the engine.

Check out these examples if you’d like to keep reading about it. They’re very interesting.

Now consider that power. How can you use it and why aren’t you already?

Where is Prolog really shining right now?

I understand how you might see this and consider it useless to your day-to-day — especially if you’re a front-end developer or work developing automation tools in Node.js. That’s fair and not every language is meant to be used in every scenario. We all know that. However, there is a place where this type of logic-based programming works and works great: pattern matching.

One key field inside AI is understanding how to use pattern matching if you’re trying to understand natural language, and that is where Prolog shines. Mind you, there are tons of other key fields inside AI. I’m just stating that Prolog is very useful inside one of them.

So given this fact, where does Prolog shine? Where could you use it if you wanted to? Well, if you deal with problems such as scheduling (which, if you haven’t tried it yet, is quite hard to solve), digital circuit verification, traffic control, or any other type of constraint logic programming in your day-to-day, then you might want to take a look at it.

In particular for AI, if you’re into Answer Set Programming or NLP (as in Natural Language Processing), Prolog is also very helpful. Giving you an example of what that might look like might be too much for an article like this, considering the depth of knowledge required, and since I’m not a Prolog expert, I would ask you to just trust me on this one. If I’ve managed to pique your interest, then go check some examples.

Who’s really using Prolog then?

We do want to answer this question after all, don’t we?

Given the data presented so far, it is safe to assume there are groups using Prolog, but they’re not as commonly known to everyone due to the fact that their field is not as public. And even if their projects are known, they don’t go around shouting everywhere that they used Prolog to create it since Prolog is usually a small part of the whole. Nevertheless, consider the following examples:

TerminusDB: This is a model-driven, revision-controlled RDF database capable of providing many of the features and behaviors Git does for version management, but for large data sets. It provides a standard RESTful API that allows you to interact with it.

IBM’s Watson: That’s right, one of IBM’s flagship projects is also using Prolog internally. Of course, it wasn’t built solely on Prolog like TerminusDB, but it does use it. As you may or may not know, Watson is meant to act as an assistant to human experts by allowing you to ask for specific information or answer simple queries. It reached the press when it famously defeated the major champions of a famous TV Show called Jeopardy. The team behind IBM used Prolog to parse natural language and translate the questions asked by humans into a form usable and understandable by Watson.

GeneXus: This low-code development platform uses Prolog to transform and translate your desired functionalities into code (it generates code for you). It can generate code in many languages including Java, Ruby, C#, Objective-C, and more.

As you can see, there is a range of use cases if you take a look at the overall product, but Prolog being just a part of these systems can be of great help for a set of very hard-to-solve problems.

Mind you, if you check out GitHub and do a quick “prolog” search, you’ll notice there are over 7,000 repositories that claim Prolog as one of their languages, so there are many out there trying to use it. The issue is (in my opinion) that not many of them have been able to build successful commercial products, making them well known (or even known) to the public.