Prolog Day 1

My experience with day one of exploring Prolog through Seven Languages in Seven Weeks.

The next language is Prolog which I’ve had no experience with and the opening paragraph instantly entices me “sometimes spectacularly smart, other times just as frustrating. You’ll get astounding answers only if you know how to ask the question.” It sounds like this is going to be a challenging week and a complete departure from the type of language I’m used to.

Io and Ruby are imperative languages while Prolog is a declarative language. Imperative means that you program the execution flow, you focus on the directions for solving a problem. You use control structures, loops and state of variables to define the flow. Declarative focuses on defining what something is rather than what it should do. You describe the desired characteristics of a solution. It is up to Prolog to figure out how to achieve these characteristics. It’s quite a change of mindset.

I think a little bit of background is useful before getting stuck in. Prolog was created 40 years ago by Alain Colmerauer and Phillipe Roussel. It is a logic programming language which works on data consisting of logical rules and relationships. You can think of logical rules as: Facts: basic assertions like “pigs like mud” Rules: inference about facts in world “an animal likes mud if it is a pig” Query: question about world “does babe like mud?” The facts and rules go into a knowledge base which is turned into an efficient form for querying by the Prolog compiler.

Note: There are different Prolog dialects. I’m going to use SWI Prolog v6.2.1

Let’s start with a very basic example modelling a mother and child relationship. You can see I’ve written a number of facts defining who is a child of who. I’ve then created a rule to define if someone is a sibling of another person. In order to be a sibling the two people need to have the same parent. I then run a number of queries to determine child-parent and sibling relationships.

We can use Prolog to find all possible matches for a query.

We’ve defined food type and flavours as facts. We have a rule which allows us to determine the flavour of a given food. It determines the food type and uses this to work out the flavour.

We’ve passed Prolog a variable ‘What’ and asked it to populate it with a food whose type is meat. The first value it has returned is spam. We can then use ; to return further values. Yes indicates that there are no more alternatives remaining. No indicates it can’t determine if there are more alternatives without doing more computation.

In the above example we’re asking Prolog “What foods have a savoury flavour?”

All the examples before have been quite simple and easy to understand. The following example taken out of the book gives a neat example of what Prolog can determine without you writing any algorithms. All you have to do is understand how to express the problem in terms of facts, rules and queries.

In the following example we use Prolog to determine how to colour a map of the southeastern United States so that two states of the same colour do not touch.


Here are my answers to the questions at the end of the chapter


Some free Prolog tutorials

A support forum (there are several)

One online reference for the Prolog version you’re using


Make a simple knowledge base. Represent some of your favorite books and authors.

Find all books in your knowledge base written by one author.

Make a knowledge base representing musicians and instruments. Also represent musicians and their genre of music.

Find all musicians who play the guitar.