Prolog on the Pi

The story of Prolog is an intriguing ‘tale of four cities’: Montréal, Marseilles, Edinburgh, and Grenoble (mainly the middle two) that began in 1970.

Originally, it was a project to make deductions from the automated reading of French text, which combined two areas of computation: natural language and logic. This is where the name comes from, ‘PROgrammation en LOGique’. During the 1970s, the project evolved into a full-blown programming language, and spread in popularity quickly without much commercial promotion.

Prolog is a declarative language. It works by first adding (‘declaring’) a set of facts and rules, commonly called a ‘knowledge base’. Then, this knowledge can be queried for ‘goals’, arrived at by logical deduction and inferencing. Prolog is an abstract environment that is totally removed from the underlying hardware, making it quite approachable for those who have little experience with computers. In fact, those who do have such experience often have greater difficulty in grasping Prolog! Procedural, functional, and object oriented programming are only supplemental tools, if available at all. In some ways, this is similar to the way SQL can be used to query a relational database.

Here is a tiny knowledge base. If you type it into a text file and save it as “”, you can use it as a test (see below).

%% a simple Prolog knowledge base
%% facts
orbits(earth, sun).
orbits(saturn, sun).
orbits(titan, saturn).
%% rules
satellite(X) :- orbits(X, _).
planet(X) :- orbits(X, sun).
moon(X) :- orbits(X, Y), planet(Y).

Prolog was a high water mark in the attempt at symbolic AI. This was the idea that formal logic and reasoning were the basis of cognition and intelligence. Since that time, emergent complexity has become a more popular research focus. However, logic is seldom a bad thing, especially when it comes to computational thinking, and learning in general.

Like most older languages, Prolog has versions that run very well on the Raspberry Pi, which would have been a supercomputer back in the 1970s. Happily, there is today a very active and deep Prolog community at SWI-Prolog (SWI is an acronym for ‘Social Science Informatics’ in Dutch). There you will find everything you need to learn and use Prolog.

Picture Permission of Raspberry Pi Foundation

To install (along with any necessary dependencies):

sudo apt-get install swi-prolog

To enter swi-prolog, load the tiny knowledge base (if you’ve saved the example from above), ask questions about Titan, and then exit:


Of course, moons ultimately orbit the sun also. I’ll leave it as an exercise for the reader to add facts and/or rules to better distinguish planets and moons.
Caveat: the Pluto can-of-worms lurks 🙂