Prolog is a logic programming language. As such, it can seem downright bizarre to people who are experienced with things like C++ or Perl. The idea is very simple. In a program you define facts and rules (the relationships between the facts). And based upon this, you can perform queries. Yet with these simple functions, a great deal can be done
To give you some idea of the power of Prolog, it was designed to do natural language processing. You have probably heard of ELIZA — one of the first chatbots ever written. That is the kind of thing Prolog was designed to do.
In fact, there are Prolog versions of ELIZA online. But this is just the start. It is widely used in a number of areas that we think of broadly as “artificial intelligence,” especially in expert systems.
How Prolog Works
Prolog can be confusing for people who have experience with procedural languages. Although it certainly has procedural aspects to it, it is mostly a matter of creating a system of facts and how they relate. Then you let Prolog manage the details of how it is done. Procedural programming usually involves asking a lot of “if” questions. Prolog has that aspect built into the data system.
To get a basic idea about Prolog, we need to talk about its three fundamental aspects: facts, rules, and queries.
- Facts: these are just what you would think. It tells the system some information. In the following example, we are declaring that Heather likes Thomas:
- Rules: these create logical expressions. They use things like the :- operator, which basically means “if:
likes(thomas,heather) :- likes(heather,thomas).
This states that Thomas likes Heather if “Heather likes Thomas” is true. Note that the original fact is an implicit rule that could have been written as:
likes(heather,thomas) := true.
- Queries: these allow us to ask the system questions. For example:
This asks the question, “Does Thomas like Heather?” And Prolog would answer back, “yes.” This is because we first established that Heather liked Thomas with a fact. Then we created a rule that stated that Thomas liked Heather if she liked him.
Another important aspect of Prolog is its use of variables. You may have noticed how all of the examples provided thus far are lower case. In Prolog, an element (called an “atom”) that begins with an upper case letter is a variable. So imagine that we wanted to find out what friends Heather had, we could create a variable
Friends to do this:
This asks the question, “Who does heather like?” And Prolog loads
Friends with whatever has been defined. In our case, it is Thomas. But it could be a list of people.
Now that you have a basic idea of what Prolog is all about, let’s look at some resources for you to get going as a logic programmer.
- Introduction to Prolog: this is a very simple tutorial, but one which deals with some basic issues that other tutorials tend to gloss over.
- Try Logic Programming! A Gentle Introduction to Prolog: this is focused on logic programming as a different programming paradigm.
- Prolog :- Tutorial: this is a detailed, example oriented introduction to the language.
- Learn Prolog Now: this is a thorough tutorial that starts at the beginning and gets into advanced subjects.
In addition to the standard tutorials, there are number of good video tutorials on Prolog. They are a surprisingly easy way to learn the fundamentals.
- Derek Banas’s Prolog Tutorial: this is an excellent hour-long video tutorial that gets you going from the very beginning: installing Prolog on your computer.
- Programming In Prolog: this is The Simple Engineer’s four part video introduction using SWI-Prolog, which runs on just about everything.
- Logic Programming: this is part of a lecture series by by Pallab Dasgupta on artificial intelligence. This one is on logic programming over all. There is also a follow-up lecture on Prolog specifically.
If you are really interested, you may want to check out a book, which can take you through the subject more thoroughly.
- Prolog Programming Success in a Day by Sam Key: a tiny book that does introduce Prolog in a day (or evening).
- Programming in Prolog by Clocksin Mellish: this is a thorough yet concise introduction to Prolog.
- Prolog Programming for Artificial Intelligence by Ivan Bratko: despite what it sounds like, this is an introduction to Prolog — and one that goes very deeply into the subject.
- The Art of Prolog: Advanced Programming Techniques by Sterling and Shapiro: this is a classic, but not an introduction. It’s something you should check out after you are comfortable with the language.
There are various free Prolog compilers and interpreters available.
- Toy Prolog Interpreter: this is an online interpreter from Monash University that is a great tool for checking out little bits of code. It’s especially good for using with tutorials before you go to the bother of installing a compiler on your computer.
- SWI-Prolog: this is a widely used Prolog implementation available for pretty much every platform.
- GNU Prolog: this is Daniel Diaz’s free compiler.
- Free Prolog Compilers and Implementations: The Free Country’s list of other Prolog implementations.
There are lots of other resources available. Some of it dates back a long way, so be aware that some of the information can be out of date.
- Prolog Resource Guide: this is quite an old resource, but it has a vast amount of useful information in it.
- Real World Programming in SWI-Prolog: this has lots of information for SWI-Prolog coders, but it much of it will be useful for people who use other environments. Of particular interest is the Frequently Asked Questions for ##Prolog.
- Prolog WikiBook: this is an excellent overview of the language with a lot of advanced material.
- Stack Overflow Prolog: this is the online forum for programmers with an enormous amount of information. Also check out the Prolog Tagged Questions page.
- SWI-Prolog Google Group: this is not a terribly active group, but there are still a lot of knowledgeable people on it. Also check out the comp.lang.prolog group.
Whether you want to focus on it or just use it to complement your other programming tools, Prolog is a great language to study. As a logic programming language, it gets you to think about problems in less procedural ways. With the resources here, you can learn and advance in your Prolog programming.